Which way to check if an element is checked is better? .Is (': checked') or .prop ('checked')

Both parameters .is(':checked')and .prop('checked')can be used to check whether the flag is set.

Are there any interesting / important differences between the two methods for querying the status of the check, or is it largely just a matter of personal preference?

+8
source share
3 answers

They both check the same thing.

If you use 1.6.0 or higher prop('checked')is the most direct jQuery way. jQuery does not need to parse and process the selector to figure out what to do. [Note below]

( 1.6.1) attr('checked') , 1.5.x .

. , x , , , :

if (x[0].checked) { /* ... */ }

:

if (x[0] && x[0].checked) { /* ... */ }

, , , . , , , , . , , . is(':checked'), , ( , -, - ).


. , prop , is, . prop - , ; is : WebKit, , is , WebKit , :checked ; Firefox (, , IE), is , , , Sizzle.

+9

prop('checked') ( , jQuery), checked .

is(':checked') .. :checked .

+2

It should be noted that it .prop('checked')only determines whether the first flag in the set is set, while it .is(':checked')returns true if at least 1 flag in the set is set.

Therefore, if you are trying to determine if any checkboxes are checked in a set, you can use it easier is(':checked').

0
source

All Articles