本文实例为大家分享了jQuery实现全选按钮的具体代码,供大家参考,具体内容如下
代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
<!DOCTYPE html> < html lang = "en" > < head > < meta charset = "UTF-8" > < meta name = "viewport" content = "width=device-width, initial-scale=1.0" > < title >全选练习</ title > < script src = "../js/jquery-1.8.2.min.js" ></ script > < script > $(function(){ //获取全选/不全选的checkbox var $chooseAll= $('#chooseAll') //获取所有多选框并且name=items的多选框 var $checkedAll=$(':checkbox[name=items]') $('#btn1').click(function(){ //使得所有的爱好多选框都选中 $checkedAll.prop('checked',true) //当所有爱好多选框都选中的时候全选框也选中 $('#chooseAll').prop('checked',true) }) $('#btn2').click(function(){ //使得所有的爱好多选框都不选中 $checkedAll.prop('checked',false) //当所有爱好多选框都不选中的时候全选框也不选中 $('#chooseAll').prop('checked',false) }) $('#btn3').click(function(){ //进行遍历所有爱好多选框, $checkedAll.each(function(){ //如果选择则为不选进行反选 this.checked=!this.checked; }) //这里对所有的爱好多选框进行过滤,过滤选中的, //如果全部选中就过滤掉length===0返回true,有一个没选中就返回false, $chooseAll.prop('checked',$checkedAll.filter(':not(:checked)').length===0) }) $('#btn4').click(function(){ //遍历输出选中就会输出对应的爱好 $checkedAll.filter(':checked').each(function(){ alert(this.value) }) }) $checkedAll.click(function(){ //判断在操作爱好的时候是否全选 $chooseAll.prop('checked',$checkedAll.filter(':not(:checked)').length===0) }) $chooseAll.click(function(){ //点击多选框的全选按钮,所的爱好都选中或者全不选中。 $checkedAll.prop('checked',this.checked) }) }) </ script > </ head > < body > < form action = "" method = "post" id = "form" > 你爱好的运动是?< input type = "checkbox" name = "chooseAll" id = "chooseAll" />全选/全不选 < br /> < input type = "checkbox" name = "items" id = "chooseSoccer" value = "足球" />足球 < input type = "checkbox" name = "items" id = "chooseBasketball" value = "篮球" />篮球 < input type = "checkbox" name = "items" id = "chooseBadminto" value = "羽毛球" />羽毛球 < input type = "checkbox" name = "items" id = "choosePingPong" value = "乒乓球" />乒乓球 < br /> < input type = "button" name = "" id = "btn1" value = "全选" /> < input type = "button" name = "" id = "btn2" value = "全不选" /> < input type = "button" name = "" id = "btn3" value = "反选" /> < input type = "button" name = "" id = "btn4" value = "提交" /> </ form > </ body > </ html > |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/w1256466374/article/details/111997191