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

时间:2021-10-17 18:14:23

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

但是在html源代码中,根本没有设置属性。如果我尝试将输入类型设置为“button”或其他类型,它就会工作。我不能用一个隐藏的输入域来做。我做错了什么?

11 个解决方案

#1


25  

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());
})

#2


24  

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设置的,但是当我查看源代码时,并没有显示新的值。我尝试了这个(多亏了费明):

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

and it displayed the modified value.

它显示了修改后的值。

#3


22  

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/pass -value-to-hidden-form-field。

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

而不是美元(“#数据”).val(数据);其中隐藏字段ID为“Data”

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

使用$(“输入[name =数据]”).val(数据);

Works perfectly for me

完全适合我

#4


3  

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来测试它(如前面的文章所建议的)。

#5


2  

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

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

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

Hope it helps.

希望它可以帮助。

#6


1  

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源文件或您有多个带有foo ID的元素。

#7


1  

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.

隐藏输入域也有同样的问题。Id我将初始值设为0,这很好,但有时我不想初始值为0。所以我用value=" "来测试,这是一个空格,它对我也适用。如果从PHP动态填充值,可以使用value=" " if 0 is fine for you, or value="

#8


1  

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") });

#9


0  

None of the above solutions worked for me.

以上的解决方案对我都不起作用。

I had to do the following to make it work:

我必须做以下的工作:

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

#10


0  

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.

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

#11


0  

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

我已经确认,设置隐藏字段的值对于jQuery选择器来说是行不通的……

$('[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.

*使用CSS隐藏字段。

<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

显然,使用隐藏字段的id还有其他问题……使用jQuery的“.val()”模式设置隐藏字段的值是无效的。

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

注意:请确保在控制台中获取值,而不是检查。

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

#1


25  

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());
})

#2


24  

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设置的,但是当我查看源代码时,并没有显示新的值。我尝试了这个(多亏了费明):

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

and it displayed the modified value.

它显示了修改后的值。

#3


22  

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/pass -value-to-hidden-form-field。

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

而不是美元(“#数据”).val(数据);其中隐藏字段ID为“Data”

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

使用$(“输入[name =数据]”).val(数据);

Works perfectly for me

完全适合我

#4


3  

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来测试它(如前面的文章所建议的)。

#5


2  

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

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

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

Hope it helps.

希望它可以帮助。

#6


1  

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源文件或您有多个带有foo ID的元素。

#7


1  

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.

隐藏输入域也有同样的问题。Id我将初始值设为0,这很好,但有时我不想初始值为0。所以我用value=" "来测试,这是一个空格,它对我也适用。如果从PHP动态填充值,可以使用value=" " if 0 is fine for you, or value="

#8


1  

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") });

#9


0  

None of the above solutions worked for me.

以上的解决方案对我都不起作用。

I had to do the following to make it work:

我必须做以下的工作:

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

#10


0  

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.

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

#11


0  

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

我已经确认,设置隐藏字段的值对于jQuery选择器来说是行不通的……

$('[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.

*使用CSS隐藏字段。

<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

显然,使用隐藏字段的id还有其他问题……使用jQuery的“.val()”模式设置隐藏字段的值是无效的。

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

注意:请确保在控制台中获取值,而不是检查。

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