Use jquery to get the value of the selected state of a group or a single checkbox

Use jquery to get the value of the checked state of a group or a single checkbox. The following is an example to illustrate. Assuming that an existing page has a group of checkboxes whose name value is id, the method to obtain the value of this group of checkboxes with name=id is as follows:

  1. var id_array=new Array();  
  2. $('input[name="id"]:checked').each(function(){  
  3.     id_array.push($( this ).val()); //Add elements to the array  
  4. });  
  5. var  idstr=id_array.join( ',' ); // join array elements to build a string  
  6. alert(idstr); 

In addition, jquery determines whether a single checkbox is selected and the method for obtaining the selected value is as follows:

 

  1. if($("#id").is(":checked")){//选中  
  2.     alert($( "#id" ).val()); // print the selected value  

 

 

Three methods for jquery to judge checked:
.attr('checked); //See version 1.6+ return: "checked" or "undefined"; 1.5- return: true or false
.prop('checked'); //16+ :true/false
.is(':checked'); //all versions:true/false//don't forget the colon

Several ways to write jquery assignment checked:
all jquery versions can be assigned like this:
// $("#cb1").attr("checked","checked");
// $("#cb1").attr( "checked",true);
jquery1.6+: 4 kinds of assignment of prop:
// $("#cb1").prop("checked",true);//It's very simple to say it
// $( "#cb1″).prop({checked:true}); //map key-value pair
// $("#cb1″).prop("checked",function(){
return true;//function returns true or false
});

//Remember this: $("#cb1").prop("checked","checked");

 

checkbox click and change events

method 1:

$("#ischange").change(function() { 
alert("checked"); 
});

Method 5:

$(document).ready(function(){ 
    $("testCheckbox").change(function() { 
         alert("Option changed!"); 
    }); 
});

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326394133&siteId=291194637