无法使用 jQuery 设置隐藏输入字段的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2109322/
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 12:48:02  来源:igfitidea点击:

Can't set hidden input field's value using jQuery

jqueryhidden-field

提问by framomo86

I have a simple input field inside a form tag:

我在表单标签中有一个简单的输入字段:

<body>
  <form action="#">
      <label>Input</label>
      <input type="hidden" id="foo" name="foo" />

  </form>
</body>

I tried to set this value from a js file:

我试图从一个 js 文件中设置这个值:

$(document).ready(function(){
    $('#foo').val('foo')
})

but in the html source the attribute isn't set at all. If I try to set the input type to "button" or anything else, it works. I just can't do it with a hiddeninput field. What am I doing wrong?

但在 html 源中,该属性根本没有设置。如果我尝试将输入类型设置为“按钮”或其他任何内容,它就可以工作。我只是不能用hidden输入字段来做到这一点。我究竟做错了什么?

采纳答案by Fermin

Can you retrieve the value from the hidden field if you set the val tag?

如果你设置了 val 标签,你能从隐藏字段中检索值吗?

e.g.

例如

<input type="hidden" id="foo" name="foo" value="bar" />

$(document).ready(function(){
  alert($('#foo').val());
})

回答by framomo86

I figured it out. The value was set by jQuery but when I viewed the source the new value wasn't shown. I tried this (thanks to Fermin):

我想到了。该值是由 jQuery 设置的,但是当我查看源代码时,未显示新值。我试过这个(感谢 Fermin):

$('#foo').val('poo')
alert($('#foo').val())

and it displayed the modified value.

并显示修改后的值。

回答by Jacob Tomlinson

The best answer I found was in the form http://forum.jquery.com/topic/passing-value-to-hidden-form-field.

我找到的最佳答案是http://forum.jquery.com/topic/passing-value-to-hidden-form-field的形式。

Instead of $('#Data').val(data);Where the hidden field ID is 'Data'

而不是$('#Data').val(data);隐藏字段 ID 是“数据”的地方

Use $('input[name=Data]').val(data);

$('input[name=Data]').val(data);

Works perfectly for me

非常适合我

回答by Stevie G

I have also been struggling with this. If you set an initial value say '0', and then watch the DOM using something like Firebug you will notice that the DOM then updates. For some reason if the initial value is set to null or an empty string, the DOM does not update, but the value is actually still stored.

我也一直在为此苦苦挣扎。如果您将初始值设置为“0”,然后使用诸如 Firebug 之类的工具观察 DOM,您会注意到 DOM 随后会更新。出于某种原因,如果初始值设置为 null 或空字符串,DOM 不会更新,但该值实际上仍被存储。

You can test this by alerting the val after you have set it (as suggested in earlier posts)

您可以通过在设置后提醒 val 来测试这一点(如之前的帖子中所建议的那样)

回答by Remo Harsono

I've had this problem when POST-ing my form and my PHP script couldn't get POST-ed value. I used this:

POST-ing 我的表单和我的 PHP 脚本无法获得POST-ed 值时,我遇到了这个问题。我用过这个:

$('#elemId').attr("name", theValue);

Hope it helps.

希望能帮助到你。

回答by Giridhar Karnik

I know that it's to late but still this might help someone.... If none of the above solutions work, just do this...

我知道为时已晚,但这仍然可能对某人有所帮助......如果上述解决方案均无效,请执行此操作......

$(document).ready(function() {
$('#foo').attr("value","foo") });

回答by mtom

I had the same problem with a hidden input field. Id i set the initial value on 0, it works fine, but sometimes i didn't want to have an initial value of 0. So i tested with value=" ", a whitespace, and it works for me too. If you fill the value dynamic from PHP you could do value="<?= $val+0; ?>"if 0 is fine for you, or value="<?= $val; ?> "if a whitespace is fine.

我对隐藏的输入字段有同样的问题。我将初始值设置为 0,它工作正常,但有时我不想将初始值设置为 0。所以我用 value="",一个空格进行了测试,它也适用于我。如果您从 PHP 填充动态值,您可以value="<?= $val+0; ?>"在 0 适合您的情况下执行此操作,或者value="<?= $val; ?> "如果空格很好。

回答by Andrew G. Johnson

I'd need to see the whole file but my guess is either you aren't including the jQuery.js source file or you have multiple elements with an ID of foo

我需要查看整个文件,但我的猜测是您没有包含 jQuery.js 源文件,或者您有多个 ID 为 foo

回答by Venkatesh Nannan

None of the above solutions worked for me.

以上解决方案都不适合我。

I had to do the following to make it work:

我必须执行以下操作才能使其正常工作:

$('#foo').val(data).blur();

回答by Noris

Rather then initialising your value attribute with an string initialise your input like this.

而不是用字符串初始化您的 value 属性,而是像这样初始化您的输入。

<input type="hidden" name="foo" id="foo" value="${foo}">

"foo" will contain the new value you set it too.

“foo”也将包含您设置的新值。