在jQuery 1.7中取消选中复选框的正确方法是什么? [英] What is the proper way to uncheck a checkbox in jQuery 1.7?

查看:71
本文介绍了在jQuery 1.7中取消选中复选框的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从jQuery 1.5.1升级-我已经了解了使用

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 what is the correct/preferred way to remove a checkbox?

这两种方法似乎都有效

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

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

这两种方法之间有区别吗?我应该使用哪个?

Is there a distinction between these methods? Which should I be using?

谢谢

推荐答案

根据 http://api.jquery .com/removeprop/ .removeProp不应用于删除选中的内容. (因为它已被完全删除,因此无法再次添加.)

According to http://api.jquery.com/removeprop/ .removeProp should not be used to remove checked. (because it is totally removed and can't be added back again.)

.removeProp()方法删除由.prop()方法设置的属性.

The .removeProp() method removes properties set by the .prop() method.

有了DOM元素或窗口对象的某些内置属性, 如果试图删除浏览器,浏览器可能会产生错误. 财产. jQuery首先将undefined的值赋给该属性,然后 忽略浏览器生成的任何错误.一般来说,它只是 删除已在对象上设置的自定义属性所必需的, 而不是内置的(本机)属性.

With some built-in properties of a DOM element or window object, browsers may generate an error if an attempt is made to remove the property. jQuery first assigns the value undefined to the property and ignores any error the browser generates. In general, it is only necessary to remove custom properties that have been set on an object, and not built-in (native) properties.

注意:请勿使用此方法删除本机属性,例如 选中,禁用或选中.这将删除该属性 完全删除,一旦删除就不能再次添加到element中.使用 .prop()将这些属性设置为false.

Note: Do not use this method to remove native properties such as checked, disabled, or selected. This will remove the property completely and, once removed, cannot be added again to element. Use .prop() to set these properties to false instead.

这篇关于在jQuery 1.7中取消选中复选框的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆