[英]How to populate an HTML field using javascript?
I have this script to pick a date in a form: 我有这个脚本来选择一个日期的形式:
<script src="http://trentrichardson.com/examples/timepicker/js/jquery-ui-timepicker-addon.js"></script>
<script>
$(document).ready(function() {
$('#datetime1').datetimepicker();
});
</script>
and here is the form: 形式如下:
<form action="#" method="get">
<input type="text" name="date1" id="datetime1" value = "03/14/2012 00:00"/>
</form>
How can I combine the following script with the datepicker script so that the form is automatically populated with today's date which can then be changed? 如何将以下脚本与datepicker脚本结合使用,以便自动用今天的日期填充表格,然后可以对其进行更改?
<!--this prints current date-->
<script type="text/javascript">
<!--
var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()
document.write(month + "/" + day + "/" + year)
-->
</script>
$(function(){
var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()
$('#datetime1').val(month + "/" + day + "/" + year).datetimepicker();
});
Do this (jQuery) : 执行此操作(jQuery):
$('#datetime1').val(month + "/" + day + "/" + year);
or (JavaScript) : 或(JavaScript):
document.getElementById('datetime1').value = month + "/" + day + "/" + year;
instead of 代替
document.write(month + "/" + day + "/" + year)
The second (jQuery) options uses the .val() method to set a value of a DOM element with the id ( #
) of datetime1
. 第二个(jQuery)选项使用.val()方法设置id(
#
)为datetime1
的DOM元素的值。 And may i suggest this mozilla JavaScript tutorial as a great place to start learning 我是否可以建议将此mozilla JavaScript教程作为开始学习的好地方
If I understood your question, you are trying to pre-populate your input with the current day: 如果我理解您的问题,那么您正在尝试使用当天填充您的输入内容:
javascript: javascript:
document.getElementById('datetime1').innerHTML = month + "/" + day + "/" + year;
jquery: jQuery的:
$('#datetime1').val(month + "/" + day + "/" + year);
Use the document ready function for when the page loads and set the value then 在页面加载时使用文档准备功能,然后设置值
$(document).ready(function () {
$('#datetime1').val(month + "/" + day + "/" + year);
});
Hope this helps 希望这可以帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.