jquery获得checkbox是否选中的状态

Concerning boolean attributes, consider a DOM element defined by the HTML markup <input type="checkbox" checked="checked" />, and assume it is in a JavaScript variable named elem:

elem.checkedtrue (Boolean) Will change with checkbox state
$(elem).prop("checked")true (Boolean) Will change with checkbox state
elem.getAttribute("checked")"checked" (String) Initial state of the checkbox; does not change
$(elem).attr("checked") (1.6)"checked" (String) Initial state of the checkbox; does not change
$(elem).attr("checked") (1.6.1+)"checked" (String) Will change with checkbox state
$(elem).attr("checked") (pre-1.6)true (Boolean) Changed with checkbox state


注意:elem.getAttribute("checked")和$(elem).attr("checked");都是获得checkbox的初始的状态,并且不会因为你是否勾选而改变状态,所以判断checkbox是否勾选不能用这2个方法

According to the W3C forms specification, the checked attribute is a boolean attribute, which means the corresponding property is true if the attribute is present at all—even if, for example, the attribute has no value or is set to empty string value or even "false". This is true of all boolean attributes.

Nevertheless, the most important concept to remember about the checked attribute is that it does not correspond to the checked property. The attribute actually corresponds to the defaultChecked property and should be used only to set the initial value of the checkbox. The checked attribute value does not change with the state of the checkbox, while the checked property does. Therefore, the cross-browser-compatible way to determine if a checkbox is checked is to use the property:

最重要的概念是记住checked attribute不相当于checked property!checked attribute不会随着checkbox状态而改变,而checked property 会


  • if ( elem.checked )
  • if ( $(elem).prop("checked") )
  • if ( $(elem).is(":checked") )

The same is true for other dynamic attributes, such as selected and value

参考:http://api.jquery.com/prop/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值