How jquery Gets the value of a check box

Source: Internet
Author: User

  Get the value of the check box there are a lot of ways, too many, in this article will introduce how jquery is to get the value of the check box, interested friends do not miss

The code is as follows: <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >  <html >  <head>  <mce:style><!-- --></mce:style><style mce_bogus= "1" >     </style>  <title>js get check box selected value </title>  </head>  <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 "checkbox" type= "Test" name= "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>  </html& nbsp   JS code   code as follows: <mce:script src= "jquery. js "mce_src=" Jquery.js ></mce:script><!--This is a loading jquery.js file that can be removed if you do not use jquery-->  <mce:script Type= "Text/javascript" ><!--    function chk () {  var obj=document.getelementsbyname (' Test '); Select all name= "' Test ' objects, return the array  //Fetch to the object array, and we'll loop through it to see if it is selected   var s= ';  for (var i=0; i<obj.length; i++) {   if (obj[i].checked) s+=obj[i].value+ ', '; If selected, add value to the variable s  } //So now to detect the value of S to know the value of the selected check box   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 operations on the checkbox     1. Select All   2. Deselect all options   3. Select all Odd   4. Anti-election   5. Get all values selected     JS code     Code as follows:   $ ("document"). Ready (function () {  $ ("#btn1"). Click (Function ( ) {  $ ("[Name= ' checkbox ']. attr ("Checked", ' true ');//select  })   $ ("#btn2"). Click (function () {  $ ("[name= ' checkbox ') ]. Removeattr ("checked");//deselect All  })   $ ("#btn3"). Click (function () {  $ ([name= ' checkbox ']:even '). attr ("Checked", ' true ');//Select All Odd  })   $ ("#btn4"). Click (function () {  $ ("[name= ' checkbox ']"). each ( function () {//Reverse selection   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";  ($). Val ()); })   alert (str); })  })     HTML code:  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>louis-blog >> jQuery Operations on the 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 '),//All  })   $ (" #btn2 "). Click (function () {  $ (" [name= ' checkbox '] "). Removeattr (" Checked  })   $ ("#btn3"). Click (function () {  $ ("[name= ' checkbox ']:even"). attr ("Checked", ' All odd  })   $ ("#btn4"). Click (function () {  $ ("[name= ' checkbox ']"). each (function () {//anti-selection   if ($ (this). attr ("checked")) {  $ (a). Removeattr ("checked"); }  $ (this). else{  (" Checked ", ' true '); } })  })   $ (" #btn5 "). Click (function () {//Output selected value &NBSp 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" >& nbsp <input type= "button" id= "Btn4" value= "anti-select" >  <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 > 
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.