简体   繁体   English

jQuery:如何在隐藏字段中保持复选框选中/取消选中状态

[英]Jquery: How to hold checkbox check / uncheck state in hidden field

i have a html table which i has many checkboxes. 我有一个HTML表,其中有很多复选框。 when user click on header checkbox then all child checkbox will be checked and unchecked based on header checkbox checked state. 当用户单击标题复选框时,将根据标题复选框的选中状态选中和取消选中所有子复选框。

user can uncheck and check any child checkbox too. 用户也可以取消选中并选中任何子复选框。 i want to store child checkbox value in hidden field separated by comma. 我想将子复选框的值存储在以逗号分隔的隐藏字段中。 when child checkbox is selected then checkbox value will be store in hiiden field but if that checkbox value is in hiiden field then will not be store in hidden field. 当选中子复选框时,复选框值将存储在hiiden字段中,但是如果该复选框值在hiiden字段中,则不会存储在隐藏字段中。

when user uncheck anyone then that checkbox value will be removed from hidden field. 当用户取消选中任何人时,该复选框的值将从隐藏字段中删除。

I tried this way but not successful. 我尝试过这种方法,但没有成功。 so guide me how to achieve it. 所以指导我如何实现它。 my code as follows 我的代码如下

<table id="tbl" border="10">
<thead>
<tr>
<td><input type="checkbox" id = "chckHead" /></td>
<td>First Row</td>
<td>Second Row</td>
<td>Third Row</td>
</tr>
</thead>
<tbody>
<tr>
<td>
<input type="checkbox" class = "chcktbl" value="101"/>
</td>
<td>1</td>
<td>1</td>
<td>1</td>
 </tr>
<tr>
<td>
<input type="checkbox" class = "chcktbl"  value="102"/>
</td>
<td>2</td>
<td>2</td>
<td>2</td>
</tr>
<tr>
<td>
<input type="checkbox" class = "chcktbl"  value="103"/>
</td>
<td>3</td>
<td>3</td>
<td>3</td>
</tr>
</tbody>
</table>
<input id="hidden" type="hidden" name="hidden">

$(document).ready(function(){
  $('#chckHead').click(function () {
    $(".chcktbl").prop('checked', $(this).prop("checked"));
  });  

  $(".chcktbl").change(function() {
  var values = [];
  $('.chcktbl').each(function (index, obj) {
  alert('pop');
    if (this.checked === true) {
      values.push($(this).val());
    }
  });
  });
  alert(values.toString());

});

You forget to assign the checkboxes values to the hidden input and also, you have to do so in the header checkbox event listener. 您忘记将复选框的值分配给隐藏的input而且还必须在标头复选框事件监听器中执行此操作。

 $(document).ready(function() { var hidden = $('#hidden'); $('#chckHead').click(function() { var state = $(this).prop('checked'); if(state === false) { hidden.val(''); } var vals = []; $('.chcktbl').each(function() { $(this).prop('checked', state); if(this.checked === true) { vals.push($(this).val()); } }); hidden.val(vals.toString()); }); $(".chcktbl").change(function() { var values = []; $('.chcktbl').each(function(index, obj) { if(this.checked === true) { values.push($(this).val()); } }); hidden.val(values.toString()); }); }); 
 <table id="tbl" border="10"> <thead> <tr> <td><input type="checkbox" id="chckHead" /></td> <td>First Row</td> <td>Second Row</td> <td>Third Row</td> </tr> </thead> <tbody> <tr> <td> <input type="checkbox" class="chcktbl" value="101" /> </td> <td>1</td> <td>1</td> <td>1</td> </tr> <tr> <td> <input type="checkbox" class="chcktbl" value="102" /> </td> <td>2</td> <td>2</td> <td>2</td> </tr> <tr> <td> <input type="checkbox" class="chcktbl" value="103" /> </td> <td>3</td> <td>3</td> <td>3</td> </tr> </tbody> </table> <!--for the demo purpose, I changed the type of the input to 'text' to see the result, don't forget to change to 'hidden' again.--> <input id="hidden" type="text" name="hidden"> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> 

Hope I pushed you further. 希望我能进一步推动您。

You should be able to get all checked checkboxes with this selector: 您应该能够使用此选择器获得所有选中的复选框:

$('input[type=checkbox]:checked')

Then you can call map to get all the ids and join them 然后,您可以调用地图以获取所有ID并将其加入

string = $('input[type=checkbox]:checked').map(function(idx,element) {
    return element.value;
}).join(',')

Then just listen the onChange event for all checkboxes and set the value for the hidden field. 然后,只需侦听所有复选框的onChange事件并设置隐藏字段的值即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM