[英]Cannot set a hidden form field using JavaScript nor with jQuery
I have a Form that uses JavaScript to get the browser/client Time Zone Offset value, I then need to set that value to a Form field so that when the Form is POSTED, it will send to my backend script. 我有一个使用JavaScript获取浏览器/客户端时区偏移量值的表单,然后需要将该值设置为“表单”字段,以便在表单被发布时将其发送到我的后端脚本。
I can alert(clientTimeOffset)
with my code below and it alerts the correct value, however when I submit a form, it never set the hidden form field with that value so it never gets posted. 我可以使用下面的代码来
alert(clientTimeOffset)
,它会提醒正确的值,但是,当我提交表单时,它永远不会使用该值设置隐藏的表单字段,因此它永远不会被发布。
Below is all the code I have tried so far, any help please? 以下是到目前为止我尝试过的所有代码,对您有帮助吗?
Version 1 版本1
jQuery( document ).ready(function() {
// Set TimeZone Offset Form Value
var d = new Date();
var clientTimeOffset = (d.getTimezoneOffset()/ 60 *(-1))+5;
jQuery("#timezone").val(clientTimeOffset);
});
Version 2 版本2
jQuery( document ).ready(function() {
// Set TimeZone Offset Form Value
var d = new Date();
var clientTimeOffset = (d.getTimezoneOffset()/ 60 *(-1))+5;
var x = document.getElementById("timezone");
x.value=clientTimeOffset;
});
My Form Field... 我的表格栏...
<input type="hidden" name="timezone" id="timezone" />
I feel silly but the problem was, somehow I had 2 hidden form fields with the same #ID, so it would set the first value and then the second one would overwrite it as empty when the POST was made. 我觉得很傻,但是问题是,以某种方式我有2个具有相同#ID的隐藏表单字段,因此它将在设置POST时设置第一个值,然后第二个值将其覆盖为空。 Maybe this will help someone someday, make sure you only have 1 form field that the selector matches!
也许有一天会有帮助,请确保您只有1个与选择器匹配的表单字段!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.