Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/431.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用jquery 1.10.2选中/取消选中所有复选框_Javascript_Jquery_Checkbox - Fatal编程技术网

Javascript 使用jquery 1.10.2选中/取消选中所有复选框

Javascript 使用jquery 1.10.2选中/取消选中所有复选框,javascript,jquery,checkbox,Javascript,Jquery,Checkbox,关于这个主题有很多问题,但是没有一个能解决我的问题,因为我不知道如何在我的代码中应用它们——我是jQuery新手 这是我的HTML页面: <div id="one"> <div id="positionable2"> <div id="xyz2"> <table id="tab1"> <tr><td class="header"><input type=chec

关于这个主题有很多问题,但是没有一个能解决我的问题,因为我不知道如何在我的代码中应用它们——我是jQuery新手

这是我的HTML页面:

<div id="one">
   <div id="positionable2">
      <div id="xyz2">
         <table id="tab1">
           <tr><td class="header"><input type=checkbox id=cbselectall /></td></tr>
           <tr><td ><input type=checkbox name=case /></td></tr>
           <tr><td ><input type=checkbox name=case /></td></tr>
           <tr><td ><input type=checkbox name=case /></td></tr>
         </table>
      </div>
   </div>
</div>
请注意,表中还有其他列,带有复选框的列是第一列。单击表格标题中的复选框后,应选中数据行上的其他列,反之亦然。不知何故,这是行不通的


由于我是jQuery新手,不知何故我无法让它工作。请提供帮助。

首先,您不需要在每个选择器中指定继承人权限

$("#cbselectall").on('click', function() {

    $(this)
    .parents('table') // go to the table element
    .find(':checkbox') /* find all checkboxes (note you might need to specifiy 
                          if you have other checkboxes in the table that shouldn't get checked 
                       */
    .prop('checked', $(this).is(':checked')); /* set the checked value to be the value
                                                 of the check all checkbox */
})

JSFIDLE会有所帮助。问题是,您的代码工作正常。问题是:在包含整个页面内容的我的页面中,它不起作用。当我单击标题复选框时,其他复选框未被选中。带有复选框的“我的表”将在加载另一个表后加载。我的jquery在HTML页面的部分。有什么我做错了吗?页面加载后是否使用JavaScript或Ajax动态创建复选框?它不起作用。在加载第一个表后,将动态加载带有复选框的表。这会引起问题吗?请帮忙。谢谢你的解决方案。成功了。我需要将这个jquery放在ajax调用的回调函数中,在这里动态加载这些jquery。非常感谢。
$("#cbselectall").on('click', function() {

    $(this)
    .parents('table') // go to the table element
    .find(':checkbox') /* find all checkboxes (note you might need to specifiy 
                          if you have other checkboxes in the table that shouldn't get checked 
                       */
    .prop('checked', $(this).is(':checked')); /* set the checked value to be the value
                                                 of the check all checkbox */
})