如何从输入时间html标记获取第二个值到JavaScript变量

时间:2018-03-25 06:23:08

标签: javascript html

s1变量没有获得秒值     在这里输入代码

function fun()
{
    time=document.getElementById("time").value;
    h1=time[0];
    h2=time[1];
    m1=time[3];
    m2=time[4];
    s1=time[6];// Not Working 
    document.getElementById("print").value=h1+""+h2+""+":"+m1+""+m2+":"+s1;
}
setInterval(fun,1000);
<p>Time:<p>
<input type="time" id="time" name="time" step="1"></input><br>
<input type="text" id="print"></input>

1 个答案:

答案 0 :(得分:0)

这是你想要的吗?

function fun()
{
time=document.getElementById("time").value;
h1=time[0];
h2=time[1];
m1=time[3];
m2=time[4];
s1=time[6];
s2=time[7];
document.getElementById("print").value = h1+""+h2+""+":"+m1+""+m2+":"+s1+""+s2;
}
<p>Time:</p><input type="time" id="time" name="time" step="1"></input><br>
<input type="text" id="print"></input>