Problem: the attr method of jQuery plug-in is often used to obtain the checked attribute value. The size of the obtained value is undefined. In this case, you can use the prop method to obtain the actual value. The differences between the two methods are described below:
1. Use the prop method to obtain the checked attribute. The returned value of checked is boolean, and the value is true. Otherwise, the value is flase.
Copy codeThe Code is as follows:
<Input type = "checkbox" id = "selectAll" onclick = "checkAll ()"> select all
Function checkAll ()
{
Var checkedOfAll = $ ("# selectAll"). prop ("checked ");
Alert (checkedOfAll );
$ ("Input [name = 'check']"). prop ("checked", checkedOfAll );
}
2. if the checked attribute is not defined during initialization in the current input when the attr method is used, $ ("# selectAll") is selected or not "). attr ("checked") will return undefined;
Copy codeThe Code is as follows:
<Input type = "checkbox" id = "selectAll" onclick = "checkAll ()"> select all
If the checked attribute is defined in the initialization of the current input, $ ("# selectAll"). attr ("checked") will return checked no matter whether or not it is selected.
Copy codeThe Code is as follows:
<Input type = "checkbox" id = "selectAll" onclick = "checkAll ()" checked> select all
Function checkAll ()
{
Var checkedOfAll = $ ("# selectAll"). attr ("checked ");
Alert (checkedOfAll );
$ ("Input [name = 'check']"). attr ("checked", checkedOfAll );
}
To sum up, if jquery is used, the prop method should be used to obtain and set the checked attribute. attr should not be used.