jQuery 0.value 为 null 或不是对象对我来说没有意义
我有以下内容:
$('#TopicRowKey')[0].value,
当我使用 IE 调试器进行检查并对对象进行监视时,我看到它有一个值。但是当脚本运行时,它会出现一条消息: 0.value is null or not an object
有人可以解释一下它是否与 [0] 相关。我不太明白 jQuery 中 [0] 的用途。
<select id="Topic" name="TopicRowKey">
<option value="01">aa</option>
<option value="02">bb</option>
</select>
- 请注意,我意识到一件事是错误的。我的id名称不正确:-(我看了这么久,但在代码中没有看到它。
I have the following:
$('#TopicRowKey')[0].value,
When I check using IE debugger and do a watch on the object I see it has a value. But when the script runs it comes up with a message saying: 0.value is null or not an object
Can someone explain if it's related to the [0]. I don't really understand with jQuery what the [0] is for.
<select id="Topic" name="TopicRowKey">
<option value="01">aa</option>
<option value="02">bb</option>
</select>
- Note that I realized one thing that's wrong. My id name is not correct :-( I looked at this for so long and didn't see it in the code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
jQuery 返回一个类似数组的对象。
这意味着它是一个可以像数组一样被遍历的对象。
当您执行 $("#myId")[0] 时,它会返回页面的实际元素。
也就是说,您的 jQuery 语法是正确的。也许这个元素不支持值(例如 div),或者您在其他地方犯了错误。您必须提供完整的代码,以便我们查看实际错误所在。
这是一个工作示例的小提琴:
http://jsfiddle.net/SW5Hz/
编辑:
您正在搜索错误的 ID。它应该是
或
# 仅用于 id。如果您想搜索名称,请执行以下操作:
或
jQuery returns an array-like object.
This means it is an object that can be traversed as if it was an array.
When you do $("#myId")[0], it returns the actual element of the page.
That is, your jQuery sintax is correct. Perhaps this element does not support a value (like a div, for example), or you're making a mistake somewhere else. You'll have to provide full code for us to see what is actually wrong.
Here is a fiddle with a working example:
http://jsfiddle.net/SW5Hz/
Edit:
You're searching for the wrong id. It should be
or
The # are meant for id only. If you want to search for name, do:
or
请注意,当文档中实际不存在 ID 为 TopicRowKey 的元素时,IE 中就会出现这种情况。确保您的给定元素在运行代码时存在并且实际上具有 ID 属性。
Observe that this occurs in IE when the element with ID TopicRowKey does not actually exist in the document. Make sure that your given element exists at the time you run your code and actually has an ID attribute.
编辑:
您应该尝试
$('#TopicRowKey').val(),
[0] 用于索引。
Edited:
You should try
$('#TopicRowKey').val(),
[0] is for the index.