自动时间元素js

automatic time element js

本文关键字:js 元素 时间      更新时间:2023-09-26

我刚刚为一个仓库做了一个from,但我觉得工人不应该每次都填写日期+时间,有没有办法用js自动填充一个隐藏的时间输入?我希望完整的日期+时间是这样的:DD/MM/YYYY H:MM这是我的代码:

<html>
<iframe src ="adminview.html" scrolling="no" height ="100"></iframe>
  <!--this is the date and time script I am using to try to fill the input-->
<script>
var today = new Date();
var UTCstring = today.toUTCString();
</script>
<form action ="">
  Name of employee:<br>
<input type ="text"></input>
  Item no.:<br>
<input type ="text"></input>
  Quantity of item:<br>
<input type ="text"></input>
<!--this is the input I am trying to fill with the above js-->
<input type ="hidden" value="UTCstring"></input>
</form>
</html>

给隐藏的输入一个id(例如:dateField(,然后将其添加到脚本中-

document.getElementById('dateField').value = UTCstring;

上面的行找到隐藏的日期字段,并在其中设置值。现在,您可以使用此值用默认值填充表单。(如果你想在客户端进行(

这是更新后的代码

<html>
<iframe src ="adminview.html" scrolling="no" height ="100"></iframe>
  <!--this is the date and time script I am using to try to fill the input-->
<script>
var today = new Date();
var UTCstring = today.toUTCString();
document.getElementById('dateField').value = UTCstring; // or today.toUTCString();
</script>
<form action ="">
  Name of employee:<br>
<input type ="text"></input>
  Item no.:<br>
<input type ="text"></input>
  Quantity of item:<br>
<input type ="text"></input>
<!--this is the input I am trying to fill with the above js-->
<input id="dateField" type ="hidden"></input>
</form>
</html>