我正在尝试在加载页面时将当前时间显示为输入中的值。但是,由于某种原因它不起作用。有什么建议?谢谢你的帮助。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="Scripts/Global.js"></script>
<script src="Scripts/jquery.js"></script>
<link rel="stylesheet" type="text/css" href="Styles/Design.css">
</head>
<body>
<input type="time" id="theTime">
<script>
$(document).ready( function() {
var current = new Date();
var minute = (now.getMinutes() + 1);
var seconds = now.getSeconds();
if(minute < 10)
minute = "0" + minute;
if(seconds < 10)
seconds = "0" + seconds;
var today = now.getFullYear() + '-' + minute + '-' + seconds;
$('#theTime').val(today);
});
</script>
</body>
</html>
答案 0 :(得分:3)
使用
var now = new Date();
而不是
var current = new Date();
答案 1 :(得分:1)
您正在使用错误的变量current
,而不是
$(document).ready( function() {
var current = new Date();
var minute = (current.getMinutes() + 1);
var seconds = current.getSeconds();
if(minute < 10)
minute = "0" + minute;
if(seconds < 10)
seconds = "0" + seconds;
var today = current.getFullYear() + '-' + minute + '-' + seconds;
$('#theTime').val(today);
});
答案 2 :(得分:1)
使用valueAsDate使输入显示值
<body>
<input type="time" id="theTime">
<script>
$(document).ready( function() {
var now = new Date();
//now2 prevents the milliseconds from showing up in the input
var now2 = new Date(now.getFullYear(), now.getMonth(), now.getDate(), now.getHours(), now.getMinutes(), now.getSeconds());
$('#theTime')[0].valueAsDate = now2;
});
</script>
</body>