对于上面的复选框
document.getElementById("checkbox1").checked // -> returns true
但是
var byAppr = document.getElementById('checkbox1').value;
$(byAppr).attr('checked') // -> returns undefined
我正在 firefox 3.6 中测试这个
最佳答案
使用以下其中一项:
-
$('#checkbox1').prop('checked')
- 在 jQuery 1.6+ 中,通常是可行的方法 -
$('#checkbox1').is(':checked')
- 所有 jQuery 版本,但速度较慢 -
$('#checkbox1').attr('checked')
- 不在 jQuery 1.6 中 - 但在 1.6.1 和 <=1.5 中,不要使用它
此外,如果您已经拥有直接可用的 DOM 元素(例如,绑定(bind)到该字段的事件处理程序中的 this
),请使用 this.checked
而不是$(this)
使用上述方法之一!
关于Jquery attr 方法对于选中的复选框返回未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6528156/