2017-11-11 105 views
1

我想在輸入字段中顯示當前時間。我已經使用ID local-time。但在輸入字段中,當前時間未顯示。如何使用jQuery設置輸入字段的值

Current Time : <input type="text" id = "local-time" value= ""/> 
<script> 
setInterval(function() { 
var local = new Date(); 
var localdatetime = local.getHours() + ":" + pad(local.getMinutes()) + ":" + 
pad(local.getSeconds()); 

$('#local-time').html(localdatetime); 
}, 1000); 

function pad(t) { 
var st = "" + t; 

while (st.length < 2) 
st = "0" + st; 

return st; 
} 
</script> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

回答

2

用途只需編輯腳本look at this:改變htmlval

<script> 
setInterval(function() { 
var local = new Date(); 
var localdatetime = local.getHours() + ":" + pad(local.getMinutes()) + ":" + 
pad(local.getSeconds()); 

$('#local-time').val(localdatetime); 
}, 1000); 

function pad(t) { 
var st = "" + t; 

while (st.length < 2) 
st = "0" + st; 

return st; 
} 
</script> 
1

您試圖設置html取而代之的是input元素的value

擁有的.val().html().text()

$('#local-time').val(localdatetime); 
1
setInterval(function() { 
    var local = new Date(); 
    var localdatetime = local.getHours() + ":" + pad(local.getMinutes()) + ":" + 
    pad(local.getSeconds()); 

    $('#local-time').val(localdatetime); 
}, 1000); 

function pad(t) { 
    var st = "" + t; 

    while (st.length < 2) 
    st = "0" + st; 

    return st; 
} 
相關問題