用jQueryselect所有checkbox
我需要帮助jQueryselect器。 说我有一个标记,如下所示:
<form> <table> <tr> <td><input type="checkbox" id="select_all"/></td> </tr> <tr> <td><input type="checkbox" name="select[]"/></td> </tr> <tr> <td><input type="checkbox" name="select[]"/></td> </tr> <tr> <td><input type="checkbox" name="select[]"/></td> </tr> </table> </form>  当用户点击它时如何获得除#select_all之外的所有checkbox? 
 任何帮助将不胜感激。 
一个更完整的例子,应该在你的情况下工作:
 $('#select_all').change(function() { var checkboxes = $(this).closest('form').find(':checkbox'); checkboxes.prop('checked', $(this).is(':checked')); }); 
 当单击#select_allcheckbox时,checkbox的状态将被选中,当前表单中的所有checkbox将被设置为相同的状态。 
 请注意,您不需要从所选内容中排除#select_allcheckbox,因为它与其他所有内容具有相同的状态。 如果您出于某种原因需要排除#select_all ,则可以使用以下命令: 
 var checkboxes = $(this).closest('form').find(':checkbox').not($(this)); 
简单而干净
 $('#select_all').click(function() { var c = this.checked; $(':checkbox').prop('checked',c); }); 
由于attr()方法,最佳答案在Jquery 1.9+中不起作用。 使用prop()来代替:
 $(function() { $('#select_all').change(function(){ var checkboxes = $(this).closest('form').find(':checkbox'); if($(this).prop('checked')) { checkboxes.prop('checked', true); } else { checkboxes.prop('checked', false); } }); }); 
 $("form input[type='checkbox']").attr( "checked" , true ); 
或者你可以使用
:checkboxselect器
 $("form input:checkbox").attr( "checked" , true ); 
我已经重写了你的HTML,并提供了主checkbox的点击处理程序
 $(function(){ $("#select_all").click( function() { $("#frm1 input[type='checkbox'].child").attr( "checked", $(this).attr("checked" ) ); }); }); <form id="frm1"> <table> <tr> <td> <input type="checkbox" id="select_all" /> </td> </tr> <tr> <td> <input type="checkbox" name="select[]" class="child" /> </td> </tr> <tr> <td> <input type="checkbox" name="select[]" class="child" /> </td> </tr> <tr> <td> <input type="checkbox" name="select[]" class="child" /> </td> </tr> </table> </form> 
  $(function() { $('#select_all').click(function() { var checkboxes = $(this).closest('form').find(':checkbox'); if($(this).is(':checked')) { checkboxes.attr('checked', 'checked'); } else { checkboxes.removeAttr('checked'); } }); }); 
  $(document).ready(function(){ $("#select_all").click(function(){ var checked_status = this.checked; $("input[name='select[]']").each(function(){ this.checked = checked_status; }); }); }); 
 jQuery(document).ready(function () { jQuery('.select-all').on('change', function () { if (jQuery(this).is(':checked')) { jQuery('input.class-name').each(function () { this.checked = true; }); } else { jQuery('input.class-name').each(function () { this.checked = false; }); } }); }); 
这段代码对我很好
 <script type="text/javascript"> $(document).ready(function(){ $("#select_all").change(function(){ $(".checkbox_class").prop("checked", $(this).prop("checked")); }); }); </script> 
您只需要将类checkbox_class添加到所有checkbox
简单而简单:D
 $('.checkall').change(function() { var checkboxes = $(this).closest('table').find('td').find(':checkbox'); if($(this).is(':checked')) { checkboxes.attr('checked', 'checked'); } else { checkboxes.removeAttr('checked'); } }); 
  $("#select_all").live("click", function(){ $("input").prop("checked", $(this).prop("checked")); } }); 
$(“#select_all”)。change(function(){
$('input [type =“checkbox”]')。prop(“checked”,$(this).prop(“checked”));
});
我现在偏向于这种风格。
我已经为您的表单命名,并在您的select_all框中添加了“onClick”。
我也排除了从jqueryselect器中的“select_all”checkbox,以防止有人点击它时,互联网被炸毁。
  function toggleSelect(formname) { // select the form with the name 'formname', // then all the checkboxes named 'select[]' // then 'click' them $('form[name='+formname+'] :checkbox[name="select[]"]').click() } 
  <form name="myform"> <tr> <td><input type="checkbox" id="select_all" onClick="toggleSelect('myform')" /> </td> </tr> <tr> <td><input type="checkbox" name="select[]"/></td> </tr> <tr> <td><input type="checkbox" name="select[]"/></td> </tr> <tr> <td><input type="checkbox" name="select[]"/></td> </tr> </table> 
下面是我写的一个基本的jQuery插件,用于select页面上的所有checkbox,除了要用作切换的checkbox/元素:
 (function($) { // Checkbox toggle function for selecting all checkboxes on the page $.fn.toggleCheckboxes = function() { // Get all checkbox elements checkboxes = $(':checkbox').not(this); // Check if the checkboxes are checked/unchecked and if so uncheck/check them if(this.is(':checked')) { checkboxes.prop('checked', true); } else { checkboxes.prop('checked', false); } } }(jQuery)); 
然后只需在checkbox或button元素上调用该函数:
 // Check all checkboxes $('.check-all').change(function() { $(this).toggleCheckboxes(); }); 
面对这个问题,上述答案都不起作用。 原因是在jQuery的统一插件(与主题metronic工作)。我希望答案是有用的:)
使用jQuery Uniform
 $('#select-all').change(function() { var $this = $(this); var $checkboxes = $this.closest('form') .find(':checkbox'); $checkboxes.prop('checked', $this.is(':checked')) .not($this) .change(); }); 
一个checkbox来统治他们
对于仍然在寻找插件来控制checkbox的人来说,通过轻量级的方式来控制checkbox,对UniformJS和iCheck提供开箱即用的支持,并且当至less有一个受控checkbox未被选中时会被取消选中(当所有控制checkbox被选中当然)我创build了一个jQuery checkAll插件 。
随意检查文档页面上的例子。
对于这个问题的例子,你需要做的是:
 $( '#select_all' ).checkall({ target: 'input[type="checkbox"][name="select"]' }); 
这不是那么清楚和简单吗?