无法使用 jQuery 设置隐藏输入字段的值
我在表单标签内有一个简单的输入字段:
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
如果设置了 val 标签,是否可以从隐藏字段中检索值?
例如
Can you retrieve the value from the hidden field if you set the val tag?
e.g.
我想通了。该值是由 jQuery 设置的,但是当我查看源代码时,没有显示新值。我尝试了这个(感谢 Fermin):
它显示了修改后的值。
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):
and it displayed the modified value.
我找到的最佳答案的形式是 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
我也一直在为此苦苦挣扎。如果您设置初始值“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)
当我对表单进行 POST 操作时,我遇到了这个问题,并且我的 PHP 脚本无法获取 POST 操作的值。我用过这个:
希望有帮助。
I've had this problem when
POST
-ing my form and my PHP script couldn't getPOST
-ed value. I used this:Hope it helps.
我需要查看整个文件,但我的猜测是您没有包含 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
我在隐藏输入字段时遇到了同样的问题。
我将初始值设置为 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, orvalue="<?= $val; ?> "
if a whitespace is fine.我知道现在已经晚了,但这仍然可能对某人有帮助......如果上述解决方案都不起作用,那就这样做......
I know that it's to late but still this might help someone.... If none of the above solutions work, just do this...
上述解决方案都不适合我。
我必须执行以下操作才能使其正常工作:
None of the above solutions worked for me.
I had to do the following to make it work:
而不是用字符串初始化您的值属性,像这样初始化您的输入。
“foo”将包含您设置的新值。
Rather then initialising your value attribute with an string initialise your input like this.
"foo" will contain the new value you set it too.
我已经确认设置隐藏字段的值不适用于这样的 jQuery 选择器...
不会更新隐藏字段...有点令人震惊。
被迫使用 CSS 隐藏字段。
显然,使用隐藏字段的 id 还存在其他问题... 设置隐藏字段的值使用 jQuery 的“.val()”的表单不起作用
注意:一定要在控制台中获取值,而不是通过检查来获取。
I've confirmed that setting the value of a hidden field does not work with a jQuery selector like this...
Will not update a hidden field... kinda shocking.
Was forced to use a CSS hidden field instead.
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.