Javascript 选中或取消选中复选框

Javascript 选中或取消选中复选框,javascript,php,jquery,checkbox,Javascript,Php,Jquery,Checkbox,我有一个HTML,如下所示: <table class="responsive display table table-bordered"> <tbody> <tr> <th>Sr No</th> <th>Student Code</th> <th>Student Name</th> <th>Dat

我有一个HTML,如下所示:

   <table class="responsive display table table-bordered">
    <tbody>
     <tr>
       <th>Sr No</th>
       <th>Student Code</th>
       <th>Student Name</th>
       <th>Date Of Birth</th>
       <th>Action</th>
       <th>Select&nbsp;&nbsp;&nbsp;<a data-placement="left" rel="tooltip" title="Select All"><input id="checkAll" name="deleteselect[]" value="0" type="checkbox"></a></th>
     </tr>
    <form action="" method="post"></form>
    <tr>
      <td>1</td>
      <td>1001</td>
      <td>Rohit Singhal </td>
      <td>17-4-1988</td>
      <td> &nbsp;&nbsp;&nbsp;&nbsp;
        <a href="http://localhost/wordpress/wp-admin/admin.php?page=eMarksheet-student-list&amp;action=update&amp;id=2" rel="tooltip" title="update" class="update">
          <i class="icon-pencil"></i>
        </a> &nbsp;&nbsp; 
        <a href="http://localhost/wordpress/wp-admin/admin.php?page=eMarksheet-student-list&amp;action=delete&amp;id=2" onclick="return show_confirm();" rel="tooltip" title="Delete" class="delete">
          <i class="icon-trash"></i>
        </a>
      </td>
      <td>
        <input name="deleteselect[]" value="2" type="checkbox">
      </td>
    </tr>
    <tr>
      <td>2</td>
      <td>1003</td>
      <td>Lisa Kurdow </td>
      <td>24-7-1965</td>
      <td> &nbsp;&nbsp;&nbsp;&nbsp;<a href="http://localhost/wordpress/wp-admin/admin.php?page=eMarksheet-student-list&amp;action=update&amp;id=6" rel="tooltip" title="update" class="update"><i class="icon-pencil"></i></a> &nbsp;&nbsp; <a href="http://localhost/wordpress/wp-admin/admin.php?page=eMarksheet-student-list&amp;action=delete&amp;id=6" onclick="return show_confirm();" rel="tooltip" title="Delete" class="delete"><i class="icon-trash"></i></a></td><td><input name="deleteselect[]" value="6" type="checkbox"></td></tr>
    </tbody></table>
但当我单击selectAll复选框时,它不会选中复选框。请帮助我它缺少什么?

如果您得到
jQuery(…).prop不是一个函数
错误,这意味着您使用的是旧版本的jQuery(
<1.6
)。非常旧,建议更新jQuery。然后使用此代码:

<script type="text/javascript">
  jQuery(document).ready(function (){
    jQuery("#checkAll").change(function () {
      if (this.checked)
        jQuery("input:checkbox").attr('checked', "checked");
      else
        jQuery("input:checkbox").removeAttr('checked');
    });
  });
</script>

jQuery(文档).ready(函数(){
jQuery(“#checkAll”).change(函数(){
如果(选中此项)
jQuery(“输入:复选框”).attr('checked','checked');
其他的
jQuery(“输入:复选框”).removeAttr(“选中”);
});
});
如果您得到
jQuery(…).prop不是一个函数
错误,则表示您使用的是旧版本的jQuery(
<1.6
)。非常旧,建议更新jQuery。然后使用此代码:

<script type="text/javascript">
  jQuery(document).ready(function (){
    jQuery("#checkAll").change(function () {
      if (this.checked)
        jQuery("input:checkbox").attr('checked', "checked");
      else
        jQuery("input:checkbox").removeAttr('checked');
    });
  });
</script>

jQuery(文档).ready(函数(){
jQuery(“#checkAll”).change(函数(){
如果(选中此项)
jQuery(“输入:复选框”).attr('checked','checked');
其他的
jQuery(“输入:复选框”).removeAttr(“选中”);
});
});

您可以使用下面的代码。我测试过了,效果很好

$(文档).ready(函数(){
$(“#checkAll”).change(函数(){
$(“输入:复选框”).attr('checked',this.checked);
});
});

高级职员
学生代码
学名
出生日期
行动
挑选
2.
1003
丽莎·库多
24-7-1965

您可以使用下面的代码。我测试过了,效果很好

$(文档).ready(函数(){
$(“#checkAll”).change(函数(){
$(“输入:复选框”).attr('checked',this.checked);
});
});

高级职员
学生代码
学名
出生日期
行动
挑选
2.
1003
丽莎·库多
24-7-1965

-works-fineWorks-too-fineWorks->那么我的本地主机可能有什么问题?在单击手柄中添加一个控制台日志记录/警报以查看它是否正在执行。也请尝试
jQuery(document).ready(函数($){…})
。。。。并检查浏览器控制台中是否有任何错误-works fineWorks fine here too->然后我的LocalHost可能有什么问题在click handelr中添加控制台日志记录/警报以查看是否正在执行。也请尝试
jQuery(document).ready(函数($){…})
。。。。并检查浏览器中是否存在任何错误console@RohitashvSinghal请升级jQuery。我在这里发布了一些代码,最后的评论还没有发布。当我看到它时,我意识到发生了什么,问题出在别的地方。但你总是很犀利,我的恭维值得投上一票。所以我这么做了。干杯:)@RohitashvSinghal请升级jQuery。我在这里发布了一些代码,最后的评论还没有发布。当我看到它时,我意识到发生了什么,问题出在别的地方。但你总是很犀利,我的恭维值得投上一票。所以我这么做了。干杯:)我绝对怀疑,检查
checked=“false”
也会检查它。你能举个例子吗?伙计,使用jQuery小于
<1.6
$(这个)。是(“:checked”)
返回布尔值。所以,它起作用了。好吧,太棒了
:)
如果您能将
$(this).is(“:checked”)
更改为
this.checked,那就太好了。两者的意思相同。你明白我为什么要求更改它吗?好的,回答更新
这个
是元素,而
$(这个)
是对象。我肯定怀疑,选中
checked=“false”
也会使它被选中。你能举个例子吗?伙计,使用jQuery小于
<1.6
$(这个)。是(“:checked”)
返回布尔值。所以,它起作用了。好吧,太棒了
:)
如果您能将
$(this).is(“:checked”)
更改为
this.checked,那就太好了。两者的意思相同。你明白我为什么要求更改它吗?好的,回答更新
是元素,
$(此)
是对象。