简体   繁体   English

如何以html格式插入JS函数的结果?

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

I have a question - how insert result of JS function in html form? 我有一个问题-如何以html格式插入JS函数的结果? I need to put time (in hh:mm format) into "value" in form. 我需要将时间(以hh:mm格式)放入“值”表格中。 The JS code: JS代码:

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

The HTML form: 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>

Thank you for your attention! 感谢您的关注!

You need to load your function like this: 您需要像这样加载函数:

<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);

the $time = h + ':' + m ; $time = h + ':' + m ; adds the hours and minute to the format you want to the $time variable. 将小时和分钟添加到$time变量所需的格式。

After that the $('#clientid').val($time); 之后$('#clientid').val($time); tells JS to add that variable to the clientid 's value. 告诉JS将变量添加到clientid的值中。

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

Try this 尝试这个

 <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