2017-02-27 75 views
0

我得到一個列表,其中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>'; 

?> 
+0

'$( 'plannerlist輸入')'返回一個列表,你不能只是'.VAL()'名單。您必須獲取列表中的所有項目。 –

+0

@VitaliiStrimbanu嗯,這是真的...... –

+0

您的嘗試解決方案的實際目的是什麼?我覺得有一個更好的方式來處理這個問題,而不是使用javascript去除輸入。由於您的解決方案似乎是空字段的某種解決方法,請閱讀http://xyproblem.info/,然後思考您的問題。 – dognose

回答

1

//在你的PHP foreach中你可以這樣做嗎?

foreach ($all_property as $item) { 
if($row[$item] != NULL || $row[$item] != '') 
{ 
    echo '<input type="text" readonly value="' . $row[$item] . '">'; 
} 

}

+0

'&&' - 它應該是'和'。 '($ x!= null || $ x!='')'總是'true',因爲它不能同時是'NULL'和'''',所以一個條件總是匹配的。 :-) – dognose

+0

謝謝!完美的作品。 @dognose你的意見是什麼意思?它對我來說就像那樣。 –

+0

@TobiasGlaus這個問題很難解釋:只使用'!='時,PHP會做一些「自動魔術」渲染「'」和「null」。因此,即使它在布爾語言方面不正確,這也是有效的。看到這個片段:https://3v4l.org/Mre2U與'AND'一起嚴格比較的情況,而不是與'OR'的非嚴格比較是正確的解決方案。試想一下:你想'隱藏'元素如果它不是空的,也不是空的 - 因此你需要檢查2個條件,因此你需要「AND」。如果PHP不會這麼做,那麼它會隨時顯示內容,(1/2) – dognose

1

$('.plannerlist input')返回輸入數組。

您需要遍歷它並找到val() == ''的元素。

var inputs = $('.plannerlist input'); 
for (var i = 0; i < inputs.length; i++) { 
    var input = inputs[i]; 
    if ($(input).val() == '') { 
     $(input).remove(); 
    } 
} 
+0

感謝您的回答。不幸的是,這對我不起作用:/ –

+0

我剛剛注意到了這一點。我修復了它。它現在工作嗎? –

+0

不,它不:/我必須在開始時定義變量輸入權? ('var inputs = $('。plannerlist input').val();') –