2017-02-27 66 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();') –