2

我试图弄清楚为什么 JQuery 不使用我的选择器中的第一项。

<input type="checkbox" id="foo1" value="Hello World">

这是我期望的工作,但返回“未定义”

$("#foo1").checked

奇怪的是,这工作正常......

$("#foo1")[0].checked

我错过了什么吗?我正在使用 JQuery 1.9.1 和 Chrome。

4

1 回答 1

3

The jQuery function $( selector ) does not return an element but a jQuery object which is kind of like an array. One thing it does not have is a checked property.

If you want to get the checked property without resorting to referencing an element by index (as in your second example), you can use

$('#foo1').prop('checked')

See http://api.jquery.com/prop/

Get the value of a property for the first element in the set of matched elements

于 2013-09-09T00:40:52.897 回答