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

发布于 2024-08-18 20:14:22 字数 425 浏览 2 评论 0原文

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

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

  </form>
</body>

我尝试从 js 文件设置此值:

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

但在 html 源中根本没有设置该属性。如果我尝试将输入类型设置为“按钮”或其他任何类型,它就会起作用。我只是无法使用隐藏输入字段来做到这一点。我做错了什么?

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:

$(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 hidden input field. What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

回眸一遍 2024-08-25 20:14:22

如果设置了 val 标签,是否可以从隐藏字段中检索值?

例如

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

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

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

e.g.

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

$(document).ready(function(){
  alert($('#foo').val());
})
梦幻的味道 2024-08-25 20:14:22

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

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

它显示了修改后的值。

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):

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

and it displayed the modified value.

∞琼窗梦回ˉ 2024-08-25 20:14:22

我找到的最佳答案的形式是 http://forum.jquery.com/topic /将值传递到隐藏表单字段

而不是 $('#Data').val(data); 如果隐藏字段 ID 为“Data”,

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

非常适合我

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

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

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

Works perfectly for me

時窥 2024-08-25 20:14:22

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

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

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.

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

梦里兽 2024-08-25 20:14:22

当我对表单进行 POST 操作时,我遇到了这个问题,并且我的 PHP 脚本无法获取 POST 操作的值。我用过这个:

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

希望有帮助。

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

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

Hope it helps.

淡墨 2024-08-25 20:14:22

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

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

ゝ偶尔ゞ 2024-08-25 20:14:22

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

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.

风吹短裙飘 2024-08-25 20:14:22

我知道现在已经晚了,但这仍然可能对某人有帮助......如果上述解决方案都不起作用,那就这样做......

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

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") });
披肩女神 2024-08-25 20:14:22

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

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

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

None of the above solutions worked for me.

I had to do the following to make it work:

$('#foo').val(data).blur();
以往的大感动 2024-08-25 20:14:22

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

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

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

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

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

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

情话墙 2024-08-25 20:14:22

我已经确认设置隐藏字段的值不适用于这样的 jQuery 选择器...

$('[name="my_field"]').val('Something');

不会更新隐藏字段...有点令人震惊。

<input type="hidden" name="my_field" id="my_field">

被迫使用 CSS 隐藏字段。

<input type="text" name="my_field" id="my_field" style="display:none">

显然,使用隐藏字段的 id 还存在其他问题... 设置隐藏字段的值使用 jQuery 的“.val()”的表单不起作用

注意:一定要在控制台中获取值,而不是通过检查来获取。

$('[name="my_field"]').val();

I've confirmed that setting the value of a hidden field does not work with a jQuery selector like this...

$('[name="my_field"]').val('Something');

Will not update a hidden field... kinda shocking.

<input type="hidden" name="my_field" id="my_field">

Was forced to use a CSS hidden field instead.

<input type="text" name="my_field" id="my_field" style="display:none">

Apparently there are other issues with using the id of a hidden field as well... Set value of hidden field in a form using jQuery's ".val()" doesn't work

Note: be sure to obtain the value in the console and not by inspecting.

$('[name="my_field"]').val();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文