简体   繁体   中英

jquery attr('checked', false) bug

Using jquery version 1.8 I am able to check and uncheck a checkbox by $el.attr('checked', logic_value) :

http://jsbin.com/oyIQuJo/1/edit

Using jquery version 1.10.1 (or 2.0.2 ) it changes just once to checked then to unchecked and I can't set it checked again:

http://jsbin.com/oyIQuJo/2/edit

http://jsbin.com/oyIQuJo/3/edit

Is it a known behaviour/bug? What is the best way to do that so I can switch it checked/unchecked multiple times?

I could do $el[0].checked = logic_value ( http://jsbin.com/oyIQuJo/4/edit ), but I don't really like this solution, because then I should loop over all the elements of the selector if it has more than one element.

You have to use .prop() instead of .attr() . Because .prop() is actually made for setting properties like checked , selected etc..

The proper way would be,

$el.prop('checked', logic_value)

Please read here to get a grip over .prop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM