Note: This article from Sina Blog, because cannot collect, therefore direct copy come over the remark, after good query
Original website:http://blog.sina.com.cn/s/blog_6810dfc20101jddq.html
When using jquery v1.10.2 to get the status of a checkbox, the output is always undefined when using. attr ("checked"). Depressed, this is a bug?!
Review the documentation for the jquery API and discover:
As of JQuery 1.6, the .attr()
method returns for attributes that has not undefined
been set. In addition, .attr()
should is used on plain objects, arrays, the window, or the document. To retrieve and change DOM properties, use the. Prop () method.
The difference between attributes and properties can be important in specific situations. before JQuery 1.6, the .attr()
method sometimes took property values into account when retrieving some attributes, Which could cause inconsistent behavior. As of JQuery 1.6, the .prop()
method provides a-explicitly retrieve property values, while .attr()
retrieves a Ttributes.
Also said: v1.6 after attr (' checked ') return to checked and undefined,v1.6 before returning true and false,v1.6 can use is (': checked ') or. Prop (' checked ') To return True and False
Summarize:
(1) method of obtaining checked
. attr (' checked '):
. Prop (' checked ')://1.6+:true/false
. Is (': Checked '):
(2) Checked assignment
All jquery versions can be assigned this way:
. attr ("Checked", "checked");
. attr ("Checked", true);
jquery1.6 or more versions of:
. Prop ("checked", true);
. Prop ("Checked", "checked");
. Prop ({checked:true});
. Prop ("Checked", function () {
return true;
});
(3) note:jquery1.6 above only exists prop ();
CheckBox Operation:
<inputID= "Cbxall"type= "checkbox"title= "Select All"name= "Cbxall"onclick= "Checkall ()" /><inputID= "Cbx1"type= "checkbox"title= "Radio"name= "Cbxtype"onclick= "Checkone ()"/>
function Checkall () { var checkall = $ ("#cbxAll"). Prop ("checked"); if (Checkall) {
Select All $ (true); Else {
Deselect all $ (false);} }
function Checkone () {$ ("#cbxAll"). Prop ("Checked",false);}
var checkedboxes = $ ("input:checkbox[name= ' Cbxtype ']:checked");
jquery Gets the checkbox property checked to undefined