簡體   English   中英

如何以html格式插入JS函數的結果?

[英]How can I insert result of JS function in html form?

我有一個問題-如何以html格式插入JS函數的結果? 我需要將時間(以hh:mm格式)放入“值”表格中。 JS代碼:

function getHours() {
var d = new Date();
var h = d.getHours();
var m = d.getMinutes();
document.getElementById('clientid').value=h+" "+m;}

HTML形式:

<html>
<form method='post' action='http.example.com/some.php'>
    <input type='hidden' name='clientid' id='clientid' value="test" action="javascript:getHours()" />
</form>
</html>

感謝您的關注!

您需要像這樣加載函數:

<html>

<body onload="getHours()">
    <form method='post' action='http.example.com/some.php'>
        <input type='hidden' name='clientid' id='clientid' value="test" action="javascript:getHours()" />
    </form>

<script>
    function getHours() {
        var d = new Date();
        var h = d.getHours();
        var m = d.getMinutes();
        document.getElementById('clientid').value = h + " " + m;
    }
</script>
</body>

</html>
$time = h + ':' + m ;
$('#clientid').val($time);

$time = h + ':' + m ; 將小時和分鍾添加到$time變量所需的格式。

之后$('#clientid').val($time); 告訴JS將變量添加到clientid的值中。

您可以使用onload()方法調用函數getHours() ,但.onload()支持以下tags
<body>, <frame>, <iframe>, <img>, <input type="image">, <link>, <script>, <style>

嘗試這個

 <script> document.addEventListener("DOMContentLoaded", function(event) { var name = "Beginner"; document.getElementById("clientid").setAttribute("value", name); }); </script> <input name='clientid' id='clientid' /> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM