javascript 无法在隐藏的输入元素上设置值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3870246/
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
Can't set value on hidden input element
提问by Stazh
I have a problem setting value of an hidden input element. I've tried using jQuery and $("#SomeHiddenElement").val(sSomeValue)function, and plain JS document.getElementById("SomeHiddenElement").value = sSomeValue;but nothing works...
我在设置隐藏输入元素的值时遇到问题。我试过使用 jQuery 和$("#SomeHiddenElement").val(sSomeValue)函数,以及普通的 JS,document.getElementById("SomeHiddenElement").value = sSomeValue;但没有任何效果......
When I set the element to text type it works just fine...
当我将元素设置为文本类型时,它工作得很好......
The problem persists both in FF and IE.
该问题在 FF 和 IE 中仍然存在。
Any ideas?
有任何想法吗?
Code:
代码:
<input type="hidden" id="SomeHiddenElement" name="SomeHiddenElement" value="" />
document.getElementById("SomeHiddenElement").value = "Testing";
回答by annakata
Since I don't believe firebug has a problem updating it's DOM representation, and your code works fine in isolation, and you don't have an issue with text inputs, and I've never had a problem updating hidden inputs myself, I would suggest that something else is acting on hidden inputs to block what you're doing.
因为我不相信 firebug 在更新它的 DOM 表示时有问题,并且您的代码在隔离时可以正常工作,并且您在文本输入方面没有问题,而且我自己更新隐藏输入从来没有遇到过问题,我会暗示其他东西正在作用于隐藏的输入以阻止您正在做的事情。
I suggest you create a test page stripped of all content except what you've given us here and then incrementally add features to progress towards the state of your real page. At some point it will break and you'll at least know where the problem lies.
我建议您创建一个测试页面,除去您在此处提供给我们的所有内容,然后逐步添加功能以朝着真实页面的状态前进。在某些时候它会崩溃,你至少会知道问题出在哪里。
回答by Stazh
Guys I'm so sorry... The problem was in combination of Firebug and my post handling code... I've fixed it...
伙计们,我很抱歉......问题出在 Firebug 和我的后期处理代码的组合中......我已经修复了它......
Moral of the story: double check code and don't blindly believe Firebug.
故事寓意:仔细检查代码,不要盲目相信 Firebug。
Thanks for your trouble!
谢谢你的麻烦!
回答by Daniel Vassallo
Your code is fine.
你的代码没问题。
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Setting the value of a hidden field</title>
</head>
<body>
<input type="hidden" id="SomeHiddenElement" value="">
<script>
document.getElementById('SomeHiddenElement').value = 'Hello World';
</script>
<script>
// Will alert 'Hello World'
alert(document.getElementById('SomeHiddenElement').value);
</script>
</body>
</html>
How are you testing that the value of the hidden field is not being set? Since hidden fields are invisible, you can see what's inside when with JavaScript, or when you post the form.
您如何测试未设置隐藏字段的值?由于隐藏字段是不可见的,因此您可以在使用 JavaScript 或发布表单时看到里面的内容。
回答by Michael Mao
Can you try:
你能试一下吗:
$('#SomeHiddenElement').attr("value", "some text");
this is just a simple jQuery solution to set the attribute "value" with test text.
这只是一个简单的 jQuery 解决方案,用于使用测试文本设置属性“值”。
Edit:
编辑:
Well, try this:
好吧,试试这个:
alert($("input[type='hidden']").length);
This is to see how many hidden input elements on your page.
这是查看页面上有多少隐藏的输入元素。
回答by ReverseEMF
These both work:
这些都有效:
$('#SomeHiddenElement').attr("value", "some text");
$('#SomeHiddenElement').val("some text");
Verified using Chrome Developer Toolsand Firebug
使用Chrome 开发者工具和Firebug 进行验证
回答by zborecque
I know it's been a long time, but I had the same issue, and it took me some time to find out that I had more than one element ( to be exact) on page with the same id="" and it seemed that my JS function did not work (which I guess it was modifying value of just the first element). Anyway, this may help someone save some time investigating :)
我知道这已经很长时间了,但我遇到了同样的问题,我花了一些时间才发现页面上有多个元素(准确地说)具有相同的 id="" 并且似乎我的JS 函数不起作用(我猜它只是在修改第一个元素的值)。无论如何,这可能会帮助某人节省一些调查时间:)

