问题描述
使用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.
我有code这个片断:
I have this snippet of code:
<input id="date" name="date" value="javascript:document.write(Date()+'.')"/>
但它不工作。
任何帮助非常感谢。
推荐答案
JavaScript就无法value属性中执行。你可以做这样的事情,虽然:
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>
您很可能希望将日期格式您preFER,因为日期的默认输出()看起来像:周二2009年6月16日10点47分10秒GMT-0400(东部夏令时间)
。请参见约格式化日期信息。
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.
这篇关于自动插入日期和时间格式输入字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!