Three ways to determine checked in jQuery

 1 Three ways for jQuery to determine checked:
 2.attr('checked'): //Look at version 1.6+ and return: "checked" or "undefined"; 1.5-return: true or false
 3.prop(‘checked’): //16+:true/false
 4.is(‘:checked’): //All versions: true/false//Don’t forget the colon
 5 Several ways to write jquery assignment checked:
 6 All jquery versions can be assigned like this:
 7 // $(“#cb1″).attr(“checked”,”checked”);
 8 // $(“#cb1″).attr(“checked”,true);
 9 jquery1.6+: 4 assignments of prop:
10 // $(“#cb1″).prop(“checked”,true);//It’s so simple that I won’t go into details
11 // $(“#cb1″).prop({checked:true}); //map key-value pair
12 // $(“#cb1″).prop(“checked”,function(){
13 return true;//The function returns true or false
14 });
15 //Remember

You can use JQuery to determine whether the checkbox in the page is selected. This can be achieved with the following code:

 
 

if ($('#checkboxId').is(':checked')) { // checkbox被选中 } else { // checkbox未被选中 }

Among them, #checkboxId is the id of the checkbox. If the checkbox is selected, is(':checked') will return true, otherwise it will return false.

In addition, if you need to make the checkbox selected, you can use the following code:

 
 

$('#checkboxId').prop('checked', true);

If you need to uncheck the checkbox, you can use the following code:

 
 

$('#checkboxId').prop('checked', false);

There is this one: $(“#cb1″).prop(“checked”,”checked”);

Guess you like

Origin blog.csdn.net/tianxianghuiwei/article/details/134028541