如果在btn上未选中复选框或输入字段没有值,则在表行中显示错误 [英] Showing error in a table row if a checkbox is not checked or an input field has no value on btn click

查看:114
本文介绍了如果在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.

这是生成输入字段的PHP代码

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.

这是按钮

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

我试过:

    $("#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";

     }

    }
  }

  );

但无法正常工作

推荐答案

检查jQuery find()方法是否返回
我认为它返回的元素集合,而不是一个单一的。所以你不能得到val()set。尝试获取inputfield [0] .val )

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()

这篇关于如果在btn上未选中复选框或输入字段没有值,则在表行中显示错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆