gpt4 book ai didi

javascript - 将 gettime 保存到表单字段

转载 作者:行者123 更新时间:2023-11-27 22:55:51 24 4
gpt4 key购买 nike

我是一个相对的 JS 新手,但我有点困惑 - 对于表单提交系统,我需要用唯一的 ID 填充表单字段(可以是任何东西,只要它相对确定)是独一无二的)。

我想到使用 getTime(),因为它会产生一个足够长且唯一的数字来满足我的要求。但我似乎无法理解用这个值填充常规(隐藏)文本字段所需的代码,尽管 SO 上的几个主题提出了类似的问题。

表单和所有 HTML 都已经就位,我需要的只是 js 脚本。谁能帮帮我吗?

最佳答案

var timestamp = new Date().getUTCMilliseconds();


var now = new Date();
timestamp =now.getFullYear().toString();
// 2011
timestamp += (now.getFullMonth < 9 ? '0' : '') + now.getFullMonth().toString();
// JS months are 0-based, so +1 and pad with 0's
timestamp += (now.getDate < 10) ? '0' : '') + now.getDate().toString();

然后将该时间戳附加到您的输入

<input name="timestamp" id="input" type="hidden"/>

然后:

document.querySelector('#input').value = timestamp;

编辑:最终代码

<script>
document.onreadystatechange = function()
{
if(document.readyState == "interactive")
{
var now = new Date();
timestamp =now.getFullYear().toString();
// 2011
timestamp += (now.getFullMonth < 9 ? '0' : '') + now.getFullMonth().toString();
// JS months are 0-based, so +1 and pad with 0's
timestamp += (now.getDate < 10) ? '0' : '') + now.getDate().toString();

document.querySelector('#input').value = timestamp;
}
}
</script>

将此脚本标记放在文档底部。

关于javascript - 将 gettime 保存到表单字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37620385/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com