让时间自动出现在输入类型=“时间”中
Have Time automatically be in the input type="time"
我试图在加载页面时将当前时间显示为输入中的值。但是,由于某种原因,它不起作用。有什么建议吗?感谢您的任何帮助。
<!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>
使用
var now = new Date();
而不是
var current = new Date();
使用 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>
你使用了错误的变量使用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);
});
在此处查看工作代码
<!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() {
let now = new Date();
$('#theTime').val(now.getHours()+":"+ now.getMinutes());
});
</script>
</body>
</html>
相关文章:
- 在控制器中将输入类型时间更改为秒
- 从getTime()类型的日期获取日期和时间,以显示mySQL数据库中的日期
- 如何从html输入类型=“”中只获得Angular控制器变量中的时间值;时间”;
- 将当前日期和时间与DateTime类型的输入进行比较
- 获取angularjs中输入类型时间的值
- 成功的操作类型未出现在时间线上
- 让时间自动出现在输入类型=“时间”中
- 如何在提交之前比较两个日期时间类型的输入
- 更新输入类型时间的值(重新渲染)并使用 React 再次关注元素
- 值属性的输入类型=“时间”绑定不会使用角度更新 UI
- 输入类型“时间”,不带小时,mm:ss 格式
- 如何在角度js中计算2种HTML5时间输入类型之间的差异
- 如何在 cassandra 中更新密钥为时间戳类型的集合
- 如何通过JavaScript清除输入类型=“日期时间”
- 获取类型错误:e 未定义与 js 插件日期时间选择器
- Asp.net jTable时间字段类型
- 向下搜索时,将X轴类型从类别更改为日期时间
- 使用Edm对oData模型进行PUT方法.时间类型
- 从Mobile Safari'日期/时间类型输入字段触发哪些事件?
- 检测日期和日期时间类型的输入类型支持