jquery选择器无法从隐藏字段读取
以下jquery 1.3.2 代码工作原理:
<input type="select" value="236434" id="ixd" name='ixd' />
<script>
console.log( $('#ixd') );
console.log( $("input[name='ixd']") );
</script>
控制台显示:
[输入#ixd 236434]
[输入#ixd 236434]
但是,将输入设置为“隐藏”会阻止选择器工作。 有什么线索吗?
<input type="hidden" value="236434" id="ixd" name='ixd' />
<script>
console.log( $('#ixd') );
console.log( $("input[name='ixd']") );
</script>
控制台显示:
[]
[]
(answers aggregated into another question)
The following jquery 1.3.2 code works:
<input type="select" value="236434" id="ixd" name='ixd' />
<script>
console.log( $('#ixd') );
console.log( $("input[name='ixd']") );
</script>
Console shows:
[input#ixd 236434]
[input#ixd 236434]
However setting the input to "hidden" prevents the selectors working. Any clues?
<input type="hidden" value="236434" id="ixd" name='ixd' />
<script>
console.log( $('#ixd') );
console.log( $("input[name='ixd']") );
</script>
Console shows:
[]
[]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
不知道为什么会失败。 我在工作中经常做同样的事情,无论表单域是否隐藏,它都有效。
也许试试这个:
这会给你隐藏字段的值。 要从表单字段中获取值,需要使用
.val()
方法。Not sure why that would be failing. I do the same thing at work on a regular basis, and it works regardless of the formfield being hidden or not.
Perhaps try this:
That will get you the value of the hidden field. To get the value out of a form field, the
.val()
method needs to be used.这甚至是有效的标记吗?
看起来,即使没有显式调用
.val()
,选择可见输入也会检索它的值,而选择隐藏输入则不会::
尝试
Is that even valid markup?
It appears that selecting a visible input retrieves the value of it, even without explicity calling
.val()
, whereas selecting a hidden one does not:Try:
and
这可能更多是控制台的问题。 我进行了测试,它似乎仍然抓住了元素的实例。 我无法确切地告诉你你想在这里做什么。
如果您只是尝试验证是否找到对象,请检查 length 属性
如果您尝试获取字段的值,请使用 val 方法
最后,在您的解决方案中,DOM 可能尚未完全加载。 确保将逻辑包装在 document.ready 调用中。
This may be more of an issue with the console. I ran a test and it seems to still grab the instance of the element. I can't exactly tell what you are trying to do here.
If you are just trying to validate wether the object was found check the length property
If you are trying to get the value of the field then use the val method
Finally, its possible that in your solution the DOM hasn't fully loaded yet. Make sure you are wrapping your logic inside a document.ready call.
如果您在 ASP.Net 中执行此操作,请在运行时通过浏览器中的“查看源代码”选项检查控件的 ID。 例如,如果您的控件是在内容页中声明的,您可能会发现您的控件 ID 不是您期望的那样。 在这种情况下,它将被分配一个由其母版页驱动的 ID。 您可以使用 ASP.Net 内联语法引用其 ClientID 属性,而不是在 jQuery 中对 ID 进行硬编码,这也将保护您免受母版页或内容页控件层次结构中的任何更改的影响。
所以......
将是一个比...更好的选择
,尽管它们都可以工作。
If you're doing this in ASP.Net, check the ID of your control at runtime via the View Source option in your browser. You might find that your control ID isn't what you expect it to be if, for example, your control is declared in a content page. In this case it would be assigned an ID that's driven by its master page. Rather than hard-coding the ID in your jQuery you can refer to its ClientID property using ASP.Net inline syntax which will also protect you from any changes in the master or content page control hierarchy.
So...
...would be a better choice than ...
...although they would both work.
我遇到问题
$('#field_id').val()
没有返回任何值,因为隐藏字段是嵌套的 (body > div > form > ul > ;李>p)。 将其移出 ul 解决了问题。
I had problem where doing
$('#field_id').val()
didn't return any value because the hidden fields were nested (body > div > form > ul > li > p
). Moving it out of ul solved the problem.如果以上方法都不起作用,请尝试
If none of the above work try