One, select the checkbox by selector:
1. Set an id attribute for the checkbox, selected by the ID selector:
input
type
=
"checkbox"
< name
=
"myBox"
id
=
"chkOne"
value
=
"1" checked="checked"
/>
Jquery:
$("#chkOne").click(function(){});
2. Set a class property for the checkbox, selected by the class selector:
< input
type
=
"checkbox"
name
=
"myBox"
class
=
"chkTwo"
value
=
"1"
checked= "Checked"/>
Jquery:
$(".chkTwo").click(function(){});
3.通过标签选择器和属性选择器来选取:
<input
type
=
"checkbox"
name
=
"someBox"
value
=
"1"
checked="checked" />
input
type
=
"checkbox"
< name
=
"someBox"
value
=
"2"
/>
JQuery:
$("input[name=‘someBox‘]").click(function(){});
二、对CheckBox的操作:
以这段checkBox代码为例:
<input
type
=
"checkbox"
name
=
"box"
value
=
"0"
checked="checked" />
input
type
=
"checkbox"
< name
=
"box"
value
=
"1"
/>
<input
type
=
"checkbox"
name
=
"box"
value
=
"2"
/>
<input
type
=
"checkbox"
name
=
"box"
value
=
"3"
/>
1.遍历checkbox用each()方法:
$("input[name=‘box‘]").each(function(){});
2.设置checkbox被选中用attr();方法:
$("input[name=‘box‘]").attr("checked","checked");
In HTML, if a check box is selected, the corresponding tag is checked= "checked". But if you use jquery alert ($ ("#id"). attr ("checked") you will be prompted to be "true" instead of "checked", so determine if ("Checked" ==$ ("#id"). attr ("checked")) Is wrong, should be if (true = = $ ("#id"). attr ("checked"))
3. Get the value of the checkbox selected: $ ("input[name= ' box '][checked]"). each (function () {
if (true = = $ (this). attr ("checked")) {
Alert ($ (this). attr (' value '));
} OR: $ ("input[name= ' box ']:checked"). each (function () {
if (true = = $ (this). attr ("checked")) {
Alert ($ (this). attr (' value '));
}
The difference between $ ("input[name= ' box ']:checked") and $ ("Input[name= ' box ')" has not been tried, I tried to use $ ("input[name= ' box ')") to succeed.
4. Get the value of the unchecked checkbox:
$ ("Input[name= ' box ']"). each (function () {
if ($ (this). attr (' checked ') ==false) {
Alert (This). Val ());
}
});
5. Set the values of the checkbox's Value property:
$ (this). attr ("value", value);
Three, generally is to create a JS array to store the value of the Traversal checkbox, create a JS array method:
1. Var array= new Array ();
2. Add data to the array:
Array.push ($ (this). Val ());
3. The array is separated by "," Output:
Alert (Array.join (', '));
<input
type
=
' checkbox '
name
=
"Mybox"
class
=
"Chktwo"
value
=
"2"
/>
<input
type
=
' checkbox '
name
=
"Mybox"
id
=
"Chkone"
value
=
"2"
/>