在表单输入字段中自动插入日期和时间? [英] Auto insert date and time in form input field?

查看:27
本文介绍了在表单输入字段中自动插入日期和时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用javascript,我们如何自动将当前日期和时间插入表单输入字段中.

Using javascript, how can we auto insert the current DATE and TIME into a form input field.

我正在构建一个问题"表单来跟踪用户提交的关于某项服务的投诉.我正在收集尽可能多的信息.我想让这里的工作人员不必在每次打开表单时手动输入日期和时间.

I'm building a "Problems" form to keep track of user submitted complaints about a certain service. I'm collecting as much info as possible. I want to save the staff here from having to manually enter the date and time each time they open the form.

我有这段代码:

<input id="date" name="date" value="javascript:document.write(Date()+'.')"/>

但它不起作用.

非常感谢您的帮助.

推荐答案

Javascript 不会在值属性内执行.不过,您可以这样做:

Javascript won't execute within a value attribute. You could do something like this, though:

<input id="date" name="date">

<script type="text/javascript">
  document.getElementById('date').value = Date();
</script>

您可能希望根据自己的喜好设置日期格式,因为 Date() 的默认输出类似于:Tue Jun 16 2009 10:47:10 GMT-0400(东部夏令时间).有关格式化日期的信息,请参阅此 SO 问题.

You'd probably want to format the date as you prefer, because the default output of Date() looks something like: Tue Jun 16 2009 10:47:10 GMT-0400 (Eastern Daylight Time). See this SO question for info about formatting a date.

这篇关于在表单输入字段中自动插入日期和时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆