用jquery实现复选框checkbox的全选

来源:互联网 发布:开机速度优化 编辑:程序博客网 时间:2024/05/01 02:21

首先需要引入jquery的代码,引入方法略

js代码

$(document).ready(function() {$(".all_select_input").click(function() {if ($(this).attr("checked")) {$("input[name='checkbox1']").each(function() {$(this).attr("checked", true);});$(".all_select_input").each(function() {$(this).attr("checked", true);});} else {$("input[name='checkbox1']").each(function() {$(this).attr("checked", false);});$(".all_select_input").each(function() {$(this).attr("checked", false);});}});//当有一个复选框没被选中时,将全选的复选框设置为未选状态$("input[name='checkbox1']").click(function() {if(!$(this).attr("checked"))$(".all_select_input").each(function() {$(this).attr("checked", false);});});});


 

 

html代码:

<input type="checkbox" class="all_select_input" name="checkbox2" value="checkbox"  />全选

<input   type="checkbox" name="checkbox1" value="100005966" />

<input type="checkbox" class="all_select_input" name="checkbox2" value="checkbox"  />全选

 

原创粉丝点击