dptt66700 2016-10-12 18:08
浏览 38
已采纳

如果未选中复选框或输入字段在btn点击时没有值,则在表格行中显示错误

I have a table and dynamic form fields. I would like to show an error on the row having a checkbox and input field if the rows input field is empty and checkbox field in the row is not checked.

This is the php code that generates the input fields

    <table class="table table-striped">
                <thead>
                <tr>
                    <th>#</th>
                    <th>Description</th>
                    <th>Status</th>
                    <th>Comment</th>
                </tr>
                </thead>
                <tbody>

 foreach ($checks as $m => $check) {
    $item ="";
    $checkbox ="";
   $textinput ="";
   $displayx="";

if ($check->mandatory_customer == 1) { //mandatory customer checks
 $displayx .="<i style='color:red;'>*</i>";
  $item .=  $check->item.$displayx;
   $checkbox .='<input type="checkbox" class="1" id="chk'.$m.'">' ;
   $textinput .='<input type="text" class="1" id="txt'.$m.'">' ;

     } else { //not mandatory customer
    $item .=  $check->item;
   $checkbox .='<input type="checkbox" class="0" id="chk'.$m.'">' ;
 $textinput .='<input type="text" class="0" id="txt'.$m.'">' ;

    }

echo "<tr id='" . $m . "'>";  //error should be set here eg: style:border: 2px solid red; 
echo "<td>" . $m . "</td>";
echo "<td>" . $item . "</td>";
echo "<td>".$checkbox."</td>";
echo "<td>".$textinput."</td>";
echo "</tr>";
}
  ?>
     }

</tbody>

Currently if a checkbox is checked the input field is disabled . This is what am using to disable the input fields

$('input[type=checkbox]').change(function() {
if (this.checked) {
  $(this).closest('tr').find('input[type=text]').prop('disabled', true);
  $(this).closest('tr').find('input[type=text]').val("");
} else {
  $(this).closest('tr').find('input[type=text]').prop('disabled', false);
}

});

I have a button that on click i would like to display the error on the tr above if the ceckbox is not checked and the input field is empty how do i go about this.

This is the button

 <button class="btn btn-success" id="approve_btn">Approve</button>

I have tried:

    $("#approve_btn").on("click", function() {
     $('input[type=checkbox]').each(function() {
   if (!$(this).is(':checked')) {
     var inputfield = $(this).closest('tr').find('input[type=text]').val();
     if(inputfield ==""){
        console.log($(this));
        $(this)[0].closest('tr').style.background = "thick solid #0000FF";

     }

    }
  }

  );

but fails to work

  • 写回答

1条回答 默认 最新

  • douli4852 2016-10-12 18:24
    关注

    Check what jQuery find() method is returning. I think it returns set of elements, not a single one. So you can't get val() of set. Try to get inputfield[0].val()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀