Jquery checks whether checkbox (check box) is selected

Source: Internet
Author: User

Copy codeThe Code is as follows:
// Check whether the selected return true and no return false are selected.
Function mycheckbox (){
Var falg = 0;
$ ("Input [name = 'soft [] ']: checkbox"). each (function (){
If ($ (this). attr ("checked ")){
Falg + = 1;
}
})
If (falg> 0)
Return true;
Else
Return false;
}

This is a good method.
The following function is a supplement to the above:
Copy codeThe Code is as follows:
This is the writing method. Jq1.42.
Next, we recommend that you process the logic.
Function mycheckbox (){
Var falg = 0;
$ ("Input [name = 'soft [] ']: checkbox"). each (function (){
If ($ (this). attr ("checked ")){
Falg = 1;
Return false;
}
})
If (falg> 0)
Return true;
Else
Return false;
}

Below is a simple judgment
Jquery core judgment statement

If ($ ('input: checkbox'). attr ("checked") = true)

Everyone knows that in html, if a check box is selected as checked = "checked ".
However, if jquery alert ($ ("# id"). attr ("checked") is used, you will be prompted to be true instead of checked.
So many friends judge if ($ ("# id "). attr ("checked") = "true") This is incorrect. It should actually be if ($ ("# id "). attr ("checked") = true)

The example contains several functions.
<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">
Code

Copy codeThe Code is as follows:
<! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Script language = "JavaScript" src = "http://www.cnjquery.com/demo/jquery.js"> </script>
<Script language = "JavaScript">
<! --
$ ("Document"). ready (function (){
$ ("# 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 = "";
$ ("[Name = 'checkbox'] [checked]"). each (function (){
Str + = $ (this). val () + "" r "n ";
// Alert ($ (this). val ());
})
Alert (str );
})
})
// -->
</SCRIPT>
</HEAD>
<BODY>
<Form name = "form1" method = "post" action = "">
<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>


Copy codeThe Code is as follows:
************************/
Function clickCheckbox (){
If ($ ("# checkPathAll"). attr ("checked "))
{
$ ("Input [name = 'checkpath']"). each (function (){
$ (This). attr ("checked", true );
});
}
Else
{
$ ("Input [name = 'checkpath']"). each (function (){
$ (This). attr ("checked", false );
});
}
}

Related Article

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.