Code collection to get the value of the Radio/checkbox group using jquery _jquery

Source: Internet
Author: User
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);
})
})
-->


which
Copy Code code as follows:

$ ("[name= ' checkbox '][checked]"). each (function () {
str+=$ (This). Val () + "\ r \ n"; Alert ($ (this). Val ());
})

This part of the code in Firefox does not work, after searching the internet, found a way to use the normal, as follows:
Copy Code code as follows:

$ ("[name= ' checkbox ']:checked"). each (function () {
str+=$ (This). Val () + "\ r \ n"; Alert ($ (this). Val ());
})

That is, use the $ ("[name= ' checkbox ']:checked") method to properly access the collection of selected controls for the Radio/checkbox group
after running, you need to refresh the next
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <ptml xmlns=" http://www.w3.org/1999/xhtml "> <pead> <meta http-equiv=" Content-type "content=" text/html; charset=gb2312 "/> <title>louis-blog >> jQuery operation on checkbox </title> <script type= ' text/ JavaScript ' src= ' http://img.jb51.net/jslib/jquery/jquery.js ' ></script> <script language= ' JavaScript ' > <!--$ ("document"). Ready (function () {$ (#btn1). Click (function () {$ ("[name= ' checkbox ']"). attr ("Checked", ' True ') $ (' #btn2 '). Click (function () {$ ("[name= ' checkbox ']"). Removeattr ("checked");/deselect All}) $ ("#btn3"). Click (function () {$ (' [name= ' checkbox ']:even '). attr ("Checked", ' true ');//Select All odd Numbers}) $ ("#btn4"). Click (function () {$ ("[name= ' checkbox ']. each (function () {//reverse-Select 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 > <div > <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= "reverse select > <input type=" button "id=" btn 5 "value=" to get all selected values "> <br><br> <input type=" checkbox "name= checkbox" value= "CheckBox1" > CheckBox1 <input type= "checkbox" name= "checkbox" value= "CheckBox2" > CheckBox2 <input type= "checkbox" Name= "ch Eckbox "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= The checkBox6 "> checkbox6 </form> </div> </body> </HTML>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]


Get the selected value in the checkbox group in jquery
Copy Code code as follows:

var SelectText;
$ (": checkbox[name=yourcheckname]:checked"). each (function () {
selecttext+=$ (This). Val () + ', ';
})

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.