jquery Gets the value of the checkbox being selected _jquery

Source: Internet
Author: User
Copy code code as follows:

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
<mce:style><!--

--></mce:style><style mce_bogus= "1" >
</style>
<title>js Get check box selected value </title>
<body>
<input type = "checkbox" name= "Test" value= "0"/>0
<input type= "checkbox" name= "Test" value= "1"/>1
<input type = "checkbox" name= "Test" value= "2"/>2
<input type= "checkbox" name= "Test" value= "3"/>3
<input type = "checkbox" name= "Test" value= "4"/>4
<input type= "checkbox" name= "Test" value= "5"/>5
<input type = "checkbox" name= "Test" value= "6"/>6
<input type= "checkbox" name= "Test" value= "7"/>7
<input type = "button" onclick= "Chk ()" value= "Submit"/>
</body>

JS Code
Copy Code code as follows:

<mce:script src= "Jquery.js" mce_src= "Jquery.js" ></mce:script><!-- This is loading the Jquery.js file, and if you don't use jquery you can remove-->
<mce:script type= "Text/javascript" ><!--

function Chk () {
var obj=document.getelementsbyname (' Test '); Select all name= "' Test ' objects, returning the array
After we take the array of objects, we loop through it to see if it's being checked.
var s= ';
for (var i=0; i<obj.length; i++) {
if (obj[i].checked) s+=obj[i].value+ ', '; If selected, adds value to the variable s
}
So now to detect the value of S, you know the value of the selected checkbox.
Alert (s== '? ') You have not chosen any content yet! ': s);
}

function Jqchk () {//jquery Get check box value
var chk_value =[];
$ (' input[name= ' Test ']:checked '). each (function () {
Chk_value.push ($ (this). Val ());
});
Alert (chk_value.length==0? ') You have not chosen any content yet! ': Chk_value);
}
--></mce:script>

Several other actions on a checkbox

1. Select All
2. Cancel all elections
3. Select All Odd
4. Anti-election
5. Get all the selected values

JS Code
Copy Code code as follows:

$ ("document"). Ready (function () {
$ ("#btn1"). Click (function () {
$ ("[name= ' checkbox ']"). attr ("Checked", ' true ');/select All
})
$ ("#btn2"). Click (function () {
$ ("[name= ' checkbox ']"). Removeattr ("checked");/Cancel All selection
})
$ ("#btn3"). Click (function () {
$ ("[name= ' checkbox ']:even"). attr ("Checked", ' true ');//Select All odd numbers
})
$ ("#btn4"). Click (function () {
$ ("[name= ' checkbox ']"). each (function () {//anti-election
if ($ (this). attr ("checked")) {
$ (this). Removeattr ("checked");
}
else{
$ (this). attr ("Checked", ' true ');
}
})
})
$ ("#btn5"). Click (function () {//Output selected value
var str= "";
$ ("[name= ' checkbox '][checked]"). each (function () {
str+=$ (This). Val () + "/r/n";
Alert ($ (this). Val ());
})
alert (str);
})
})

HTML code:
Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>louis-blog >> jQuery Operations on a checkbox </title>
<mce:script type= ' text/javascript ' src= ' http://leotheme.cn/wp-includes/js/jquery/jquery.js ' mce_src= ' http:// Leotheme.cn/wp-includes/js/jquery/jquery.js "></mce: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 selection
})
$ ("#btn3"). Click (function () {
$ ("[name= ' checkbox ']:even"). attr ("Checked", ' true ');//Select All odd numbers
})
$ ("#btn4"). Click (function () {
$ ("[name= ' checkbox ']"). each (function () {//anti-election
if ($ (this). attr ("checked")) {
$ (this). Removeattr ("checked");
}
else{
$ (this). attr ("Checked", ' true ');
}
})
})
$ ("#btn5"). Click (function () {//Output selected value
var str= "";
$ ("[name= ' checkbox '][checked]"). each (function () {
str+=$ (This). Val () + "/r/n";
Alert ($ (this). Val ());
})
alert (str);
})
})
-->
</SCRIPT>
</HEAD>
<body style= "text-align:center;margin:0 auto;font-size:12px;" mce_style= "text-align:center;margin:0 auto;" font-size:12px; " >
<div style= "border:1px solid #999; width:500px; padding:15px; Background: #eee; margin-top:150px; " >
<form name= "Form1" method= "Post" action= "" >
<input type= "button" id= "BTN1" value= "Select All" >
<input type= "button" id= "Btn2" value= "deselect All" >
<input type= "button" id= "Btn3" value= "Select All Odd" >
<input type= "button" id= "Btn4" value= "anti-selection" >
<input type= "button" id= "Btn5" value= "Get All values selected" >
<br/><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
</form>
</div>
</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.