-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathjq_inputUse.html
84 lines (77 loc) · 2.48 KB
/
jq_inputUse.html
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>jq对于input选择框的使用</title>
<style type="text/css">
*{margin: 0; padding: 0; list-style: none;}
.box{ width:500px; margin:100px auto;}
</style>
<script src="jquery-1.12.0.min.js" type="text/javascript"></script>
</head>
<body>
<div class="box">
<ul id="list">
<li><label for><input type="checkbox" value="1" />1.第一夫人</label></li>
<li><label for><input type="checkbox" value="2" />2.仰望星空</label></li>
<li><label for><input type="checkbox" value="3" />3.会痛的石头</label></li>
<li><label for><input type="checkbox" value="4" />4.给我你的爱</label></li>
<li><label for><input type="checkbox" value="5" />5.逆战</label></li>
</ul>
<input type="checkbox" id="all" />
<input type="button" value="全选" id="selectAll" />
<input type="button" value="反选" id="reverse" />
<input type="button" value="获得选中的值" id="getValue" />
</div>
</body>
<script type="text/javascript">
var $all=$("#all"),
$sltAll=$("#selectAll"),
$reverse=$("#reverse"),
$getValue=$("#getValue");
$all.on("click",function(){
if(this.checked){
$("#list :checkbox").prop("checked", true);
}else{
$("#list :checkbox").prop("checked", false);
}
});
$sltAll.on("click",function(){
$("#list :checkbox,#all").prop("checked", true);
});
$reverse.on("click",function(){
$("#list :checkbox").each(function() {
$(this).prop("checked", !$(this).prop("checked"));
});
allchk();
});
$("#list :checkbox").on("click",function() {
allchk();
});
$getValue.on("click",function(){
var valArr=new Array;
// $("#list :checkbox[checked]").map(function() {
// return $(this).val();
// }).get().join(",");
$("#list :checkbox[checked]").each(function(i) {
valArr[i]=$(this).val();
});
var vals=valArr.join(",");
alert(vals);
});
function allchk(){
var chknum=$("#list :checkbox").size(),
chk=0;
$("#list :checkbox").each(function(){
if($(this).prop("checked")==true){
chk++;
}
});
if(chknum==chk){
$all.prop('checked', true);
}else{
$all.prop('checked',false);
}
}
</script>
</html>