未设置值时删除输入

时间:2017-02-27 16:34:18

标签: javascript php jquery html mysql

我有一个列表,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>';

?> 

2 个答案:

答案 0 :(得分: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();
    }
}

答案 1 :(得分:1)

//在你的php foreach中你可以这样做吗?

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

}