JQuery checks whether the checkbox (check box) is selected, selects all, and reselect the implementation code.

Source: Internet
Author: User

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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.