Jquery operates on several methods of the checked and disabled properties
- 2020-03-30 03:24:49
- OfStack
There is no difference between the following two methods except that there is less code...
<input id="cb1" type="checkbox" checked />
<input id="cb2" type="checkbox" checked="checked" />
Jquery determines checked in three ways:
.attr('checked'): //See version 1.6+ return :"checked" or "undefined"; 1.5- returns :true or false
.prop('checked'): //16+:true/false
.is(':checked'): // All versions :true/false// Don't forget the colon
Several ways to write jquery checked:
All jquery versions can be assigned like this:
$("#cb1").attr("checked","checked");
$("#cb1").attr("checked",true);
Jquery1.6 +: 4 assignments for prop:
$("#cb1").prop("checked",true); //It's easy to forget
$("#cb1").prop({checked:true}); //The map key/value pair
$("#cb1").prop("checked",function(){
return true; //The function returns true or false
});
$("#cb1").prop("checked","checked");
More reference: http://api.jquery.com/prop/
<html>
<head>
<title> test </title>
<style type="text/css">
</style>
<!--1.62 You can modify 1.42 1.52 1.7 To test the -->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
//Judge checked < br / >
// var a=$("#cb1").attr('checked'); // See the version 1.6+ return :"checked" or "undefined" ;1.5- return :true or false
// var b=$("#cb1").prop('checked'); //1.6+:true/false
var c=$("#cb1").is(':checked'); //All versions :true/false
// alert(a);
// alert(b);
alert(c);
//Assignment All of the first two jquery versions support prop except for query1.6+, which supports
// $("#cb1").attr("checked","checked");//1.5-
// $("#cb1").attr("checked",true);//1.5-
// $("#cb1").prop("checked","checked");//1.6+( I forgot this when I was tidying up )
// $("#cb1").prop("checked",true);//1.6+
// $("#cb1").prop({checked:true});//1.6+
// $("#cb1").prop("checked",function(){
// return true;//1.6+
// });
})();
</script>
</head>
<body>
<!-- Remember to get rid of it when you assign checked-->
<input id="cb1" type="checkbox" checked />
<input id="cb2" type="checkbox" checked="checked"/>
</body>
</html>