What is the proper way to uncheck a checkbox in jQuery 1.7?

前端 未结 4 1690
轮回少年
轮回少年 2021-01-05 14:30

I\'m upgrading from jQuery 1.5.1 -- I\'ve read about the \"new\" way to \"check\" checkboxes (in 1.6) using

prop(\"checked\", true);

But w

4条回答
  •  执笔经年
    2021-01-05 15:07

    To answer your question more precisely:

    I would always prefer

    $('#someSelector').prop('checked', false);
    

    over

    $('#someSelector').removeProp('checked');
    

    because an important difference between attribute and property is in this case, that removing the attribute equals to setting the property (is-checked) to false.

    Removing the "checked" property of a checkbox does not make any sense at all, because the checkbox will always be either checked or unchecked. Therefore setting the property to false to uncheck the box is logically consistent, removing the property is not.

提交回复
热议问题