Javascript 如何强制秒出现在 HTML5“时间”输入控件上?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14487621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I force seconds to appear on an HTML5 "time" input control?
提问by Joe Z.
When I use JavaScript to set the value of an HTML5 "time" object, like this:
当我使用 JavaScript 设置 HTML5“时间”对象的值时,如下所示:
document.getElementById("settime").value = "13:24:59";
It will show a time control with "13:24:59" on it, and I can change everything. But if I do this:
它将显示一个带有“13:24:59”的时间控件,我可以更改所有内容。但如果我这样做:
document.getElementById("settime").value = "13:25:00";
It hides the seconds, showing just "13:25" with no seconds. How do I force the "00" seconds to appear in this case?
它隐藏了秒数,只显示“13:25”而没有秒数。在这种情况下,如何强制出现“00”秒?
(This is in Google Chrome, by the way.)
(顺便说一下,这是在谷歌浏览器中。)
回答by j08691
Set the stepattribute.
设置step属性。
Ex: <input id="settime" type="time" step="1" />
前任: <input id="settime" type="time" step="1" />
document.getElementById("settime").value = "13:24:00";
<input id="settime" type="time" step="1" />
A note about the step attribute:
关于step 属性的说明:
The step attribute can be used with a numerical input value to dictate how granular the values you can input are. For example, you might want users to enter a particular time, but only in 30 minute increments. In this case, we can use the step attribute, keeping in mind that for time inputs the value of the attribute is in seconds. For example, a 30 minute increment would have a step value of 1800. <input type="time" step="1800">
step 属性可以与数字输入值一起使用,以指示您可以输入的值的粒度。例如,您可能希望用户输入特定时间,但只能以 30 分钟为增量。在这种情况下,我们可以使用 step 属性,请记住,对于时间输入,该属性的值以秒为单位。例如,30 分钟增量的步长值为 1800。<input type="time" step="1800">

