jQuery jquery选择器无法从隐藏字段读取

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/996644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 10:21:50  来源:igfitidea点击:

jquery selector can't read from hidden field

jqueryformscss-selectorselementhidden

提问by Andy

(answers aggregated into another question)

答案汇总到另一个问题中

The following jquery 1.3.2 code works:

以下 jquery 1.3.2 代码有效:

<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]

[输入#ixd 236434]

[输入#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:

控制台显示:

[]

[]

[]

[]

回答by Mike Trpcic

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:

也许试试这个:

<input type="hidden" value="236434" id="ixd" name='ixd' />

<script>
    console.log($("#xid").val())
</script>

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()需要使用该方法。

回答by karim79

<input type="select" value="236434" id="ixd" name='ixd' />

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:

似乎选择一个可见的输入会检索它的值,即使没有显式调用.val(),而选择一个隐藏的输入不会:

Try:

尝试:

console.log( $('#ixd').val() );
console.log( $("input[name='ixd'][type='hidden']") );

and

console.log( $("input[name='ixd']").val() );

回答by bendewey

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

如果您只是想验证是否找到了对象,请检查 length 属性

console.log( $('#xid').length );

If you are trying to get the value of the field then use the val method

如果您尝试获取字段的值,请使用 val 方法

console.log( $('#xid').val() );

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.

最后,在您的解决方案中,DOM 可能还没有完全加载。确保将逻辑包装在 document.ready 调用中。

$(document).ready(function() {
    console.log( $('#xid').val() );
});

回答by JTIsALeaf

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.

如果您在 ASP.Net 中执行此操作,请在运行时通过浏览器中的“查看源代码”选项检查控件的 ID。例如,如果您的控件是在内容页中声明的,您可能会发现您的控件 ID 不是您所期望的。在这种情况下,它将被分配一个由其母版页驱动的 ID。您可以使用 ASP.Net 内联语法引用其 ClientID 属性,而不是在您的 jQuery 中硬编码 ID,这也将保护您免受主页面或内容页面控件层次结构中的任何更改。

So...

所以...

$('#<%= myHiddenControl.ClientID %>').val();

...would be a better choice than ...

......会比......更好的选择

$('#ctl00_ContentPlaceHolder1_ctl00_ContentPlaceHolder1_myHiddenControl').val();

...although they would both work.

...虽然他们都可以工作。

回答by Ryan Soury

If none of the above work try

如果以上工作都没有尝试

$('#xid').attr('value');

回答by Lenart

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.

我遇到的问题是$('#field_id').val()do 没有返回任何值,因为隐藏字段是嵌套的 ( body > div > form > ul > li > p)。将它移出 ul 解决了问题。