jquery Gets the value of the checkbox and determines whether it is not selected

Source: Internet
Author: User

Core code

The code is as follows Copy Code

if ($ ("Input[name= ' checkbox ']:checkbox:checked '). length>0) {
box = ';
$ ("Input[name= ' checkbox ']:checkbox:checked"). each (function () {
Box + = $ (this). Val () + ', ';
})
}else{
Alert (' no choice ');
return false;
}

Cases

The code is as follows Copy Code

<input type= "checkbox" name= "Sex" value= "male"/> Male
<input type= "checkbox" name= "Sex" value= "female"/> Female
<input type= "checkbox" name= "Sex" value= "Unknow"/>unknow
<input type= "button" value= "Showselectedvalue" id= "Showvalue"/>
$ (function () {
$ ("#showValue"). Click (function () {
$ (": checkbox[name= ' Sex '][checked]"). each (function () {
Alert ($ (this). Val ());
});
})
});

Full instance

The code is as follows Copy Code

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<title>js gets the value of the check box selected </title>
<body>
<input type= "checkbox" name= "Test" value= "0"/>0&nbsp;&nbsp;
<input type= "checkbox" name= "Test" value= "1"/>1&nbsp;&nbsp;
<input type= "checkbox" name= "Test" value= "2"/>2&nbsp;&nbsp;
<input type= "checkbox" name= "Test" value= "3"/>3&nbsp;&nbsp;
<input type= "checkbox" name= "Test" value= "4"/>4&nbsp;&nbsp;
<input type= "checkbox" name= "Test" value= "5"/>5&nbsp;&nbsp;
<input type= "checkbox" name= "Test" value= "6"/>6&nbsp;&nbsp;
<input type= "checkbox" name= "Test" value= "7"/>7&nbsp;&nbsp;
<input type= "button" onclick= "Chk ()" value= "Submit"/>
</body>


<mce:script type= "Text/javascript" ><!--
 
Function chk () { 
  var obj= Document.getelementsbyname (' test '); //Select all Name= "' Test '" objects, return array  
 ///Fetch to object array, we'll loop to see if it's selected  
  var s= '; 
  for (var i=0; i<obj.length; i++) { 
    if (obj[i) . Checked) s+=obj[i].value+ ', '; //If selected, add value to variable s  
 } 
 // So now to detect the value of S, you 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); 

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.