我得到一個列表,其中PHP從mySQL創建<input type="text" readonly value="' . $row[$item] . '">';
。有時數據庫表中的字段爲空,所以輸入看起來像這樣:<input type="text" readonly value>';
因此是的一個值標記,但沒有任何值。 我試圖做的只是刪除空的輸入。 我嘗試使用此代碼:未設置值時刪除輸入
function clearEmpty(){
var input = $('.plannerlist input').val();
if(input < 0){
$('.plannerlist input').remove();
}
}
.plannerlist
是以下列表:
<ul class="plannerlist" id="plannerlist1">
<input type="text" readonly value="Some value here">
<input type="text" readonly value>
<input type="text" readonly value="or no value like above">
<input type="text" readonly value>
</ul>
我上面寫的功能不工作。沒有輸入被刪除,沒有控制檯錯誤,沒有任何... 什麼可能是問題?
那是我的PHP(我知道這是一個爛攤子,但它的工作原理;)):
<?php
$host = "********";
$user = "********";
$pass = "********";
$db_name = "********";
//create connection
$connection = mysqli_connect($host, $user, $pass, $db_name);
//test if connection failed
if(mysqli_connect_errno()){
die("connection failed: "
. mysqli_connect_error()
. " (" . mysqli_connect_errno()
. ")");
}
echo '<div id="content">
<form action="save.php" method="post">
<section class="tabcontent">
<ul class="plannerlist" id="plannerlist1">';
//get results from database
$result = mysqli_query($connection,"SELECT FR_PM FROM anmeldungen");
$all_property = array(); //declare an array for saving property
//showing property
while ($property = mysqli_fetch_field($result)) {
array_push($all_property, $property->name); //save those to array
}
echo '</tr>'; //end tr tag
//showing all data
while ($row = mysqli_fetch_array($result)) {
foreach ($all_property as $item) {
echo '<input type="text" readonly value="' . $row[$item] . '">'; //get items using property value
}
}
echo '</ul>
</section>
<section class="tabcontent">
<ul class="plannerlist" id="plannerlist2">';
//get results from database
$result = mysqli_query($connection,"SELECT SA_AM FROM anmeldungen");
$all_property = array(); //declare an array for saving property
//showing property
while ($property = mysqli_fetch_field($result)) {
array_push($all_property, $property->name); //save those to array
}
echo '</tr>'; //end tr tag
//showing all data
while ($row = mysqli_fetch_array($result)) {
foreach ($all_property as $item) {
echo '<input type="text" readonly value="' . $row[$item] . '">'; //get items using property value
}
}
echo '</ul>
</section>
<section class="tabcontent">
<ul class="plannerlist" id="plannerlist3">';
//get results from database
$result = mysqli_query($connection,"SELECT SA_PM FROM anmeldungen");
$all_property = array(); //declare an array for saving property
//showing property
while ($property = mysqli_fetch_field($result)) {
array_push($all_property, $property->name); //save those to array
}
echo '</tr>'; //end tr tag
//showing all data
while ($row = mysqli_fetch_array($result)) {
foreach ($all_property as $item) {
echo '<input type="text" readonly value="' . $row[$item] . '">'; //get items using property value
}
}
echo '</ul>
</section>
<section class="tabcontent">
<ul class="plannerlist" id="plannerlist4">';
//get results from database
$result = mysqli_query($connection,"SELECT SO_AM FROM anmeldungen");
$all_property = array(); //declare an array for saving property
//showing property
while ($property = mysqli_fetch_field($result)) {
array_push($all_property, $property->name); //save those to array
}
echo '</tr>'; //end tr tag
//showing all data
while ($row = mysqli_fetch_array($result)) {
foreach ($all_property as $item) {
echo '<input type="text" readonly value="' . $row[$item] . '">'; //get items using property value
}
}
echo '</ul>
</section>
<section class="tabcontent">
<ul class="plannerlist" id="plannerlist5">';
//get results from database
$result = mysqli_query($connection,"SELECT SO_PM FROM anmeldungen");
$all_property = array(); //declare an array for saving property
//showing property
while ($property = mysqli_fetch_field($result)) {
array_push($all_property, $property->name); //save those to array
}
echo '</tr>'; //end tr tag
//showing all data
while ($row = mysqli_fetch_array($result)) {
foreach ($all_property as $item) {
echo '<input type="text" readonly value="' . $row[$item] . '">'; //get items using property value
}
}
echo '</ul>
</section>
<section class="tabcontent">
<ul class="plannerlist" id="plannerlist6">';
//get results from database
$result = mysqli_query($connection,"SELECT MO_AM FROM anmeldungen");
$all_property = array(); //declare an array for saving property
//showing property
while ($property = mysqli_fetch_field($result)) {
array_push($all_property, $property->name); //save those to array
}
echo '</tr>'; //end tr tag
//showing all data
while ($row = mysqli_fetch_array($result)) {
foreach ($all_property as $item) {
echo '<input type="text" readonly value="' . $row[$item] . '">'; //get items using property value
}
}
echo '</ul>
</section>
<input name="plannersubmit" id="plannersubmit" type="submit">
</form>
</div>';
?>
'$( 'plannerlist輸入')'返回一個列表,你不能只是'.VAL()'名單。您必須獲取列表中的所有項目。 –
@VitaliiStrimbanu嗯,這是真的...... –
您的嘗試解決方案的實際目的是什麼?我覺得有一個更好的方式來處理這個問題,而不是使用javascript去除輸入。由於您的解決方案似乎是空字段的某種解決方法,請閱讀http://xyproblem.info/,然後思考您的問題。 – dognose