使用 JavaScript 设置隐藏表单字段值但请求仍然为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2965900/
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
Set hidden form field values with JavaScript but request still empty
提问by ownking
I try to set some hidden form field values with an onclick event. Ok, after I did something like this:
我尝试使用 onclick 事件设置一些隐藏的表单字段值。好的,在我做了这样的事情之后:
document.getElementById('hidden_field').value = 123;
I can output the value with the firebug console by entering this:
我可以通过输入以下内容使用萤火虫控制台输出该值:
alert(document.getElementById('hidden_field').value);
So the values are definitely set. But now when I submit the form, the hidden field values are still empty.
所以这些值肯定是设置好的。但是现在当我提交表单时,隐藏字段值仍然是空的。
Do you have any idea whats going wrong?
你知道出了什么问题吗?
回答by Andy E
Make sure your hidden field has the nameattribute:
确保您的隐藏字段具有以下name属性:
<input id="hidden_field" name="hidden_field" type="hidden" value="123" />
Inputs without a nameattribute aren't sent with the request.
没有name属性的输入不会随请求一起发送。

