JQuery checks whether the checkbox (check box) is selected:
If ($ ("# id"). attr ("checked") = true)
JQuery implements checkbox (check box) selection, full selection/deselection code:
Copy codeThe Code is as follows:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> How does jQuery determine whether or not checkbox (check box) is selected, all selected, or reversed? </title>
<Script type = "text/javascript" src = "jquery. js"> </script>
<Script type = "text/javascript">
<! --
$ ("Document"). ready (function (){
$ ("# All"). click (function (){
If (this. checked ){
$ ("Input [name = 'checkbox']"). each (function () {this. checked = true ;});
$ ("# Btn1"). attr ("value", "invert ");
} Else {
$ ("Input [name = 'checkbox']"). each (function () {this. checked = false ;});
$ ("# Btn1"). attr ("value", "select all ");
}
});
$ ("# Btn1"). click (function (){
$ ("[Name = 'checkbox']"). attr ("checked", 'true'); // select all
});
$ ("# Btn2"). click (function (){
$ ("[Name = 'checkbox']"). removeAttr ("checked"); // cancel all
});
$ ("# Btn3"). click (function (){
$ ("[Name = 'checkbox']: even"). attr ("checked", 'true'); // select all odd numbers
});
$ ("# Btn4"). click (function (){
$ ("[Name = 'checkbox']"). each (function (){
If ($ (this). attr ("checked "))
{
$ (This). removeAttr ("checked ");
} Else {
$ (This). attr ("checked", 'true ');
}
});
});
$ ("# Btn5"). click (function (){
Var str = "";
// $ ("Input: checkbox [name = 'checkbox']: checked"). each (function () {// Yes
$ ("[Name = 'checkbox'] [checked]"). each (function (){
Str + = $ (this). val () + "\ r \ n ";
});
Alert (str );
});
});
// -->
</Script>
</Head>
<Body>
<Form name = "form1" method = "post" action = "">
<Input type = "checkbox" id = "all" name = "all">
<Input type = "button" id = "btn1" value = "select all">
<Input type = "button" id = "btn2" value = "cancel all">
<Input type = "button" id = "btn3" value = "select all odd numbers">
<Input type = "button" id = "btn4" value = "reselect">
<Input type = "button" id = "btn5" value = "get all selected values">
<Br/>
<Input type = "checkbox" name = "checkbox" value = "checkbox1"> checkbox1
<Input type = "checkbox" name = "checkbox" value = "checkbox2"> checkbox2
<Input type = "checkbox" name = "checkbox" value = "checkbox3"> checkbox3
<Input type = "checkbox" name = "checkbox" value = "checkbox4"> checkbox4
<Input type = "checkbox" name = "checkbox" value = "checkbox5"> checkbox5
<Input type = "checkbox" name = "checkbox" value = "checkbox6"> checkbox6
<Input type = "checkbox" name = "checkbox" value = "checkbox7"> checkbox7
<Input type = "checkbox" name = "checkbox" value = "checkbox8"> checkbox8
</Form>
</Body>
</Html>