将文本从Javascript调用传递到页面加载中的表单中的文本字段?

时间:2013-05-16 22:32:48

标签: javascript forms

我以为我可以在网上找到这个解决方案,但我似乎无法找到解决方案。

我有一个带有id =“filledonload”的文本字段的表单,它需要在页面加载时从JavaScript函数中获取文本值吗?这是怎么做到的?我想更好的词就是自动“填充”

感谢您的帮助..

以下是我的超级简单表格。

<form id="form1" name="formname" method="post" action="">
<label>This Field Would Grab Text From JavaScript Function When The Page Loads
    <input type="text" name="filledonload" id="filledonload" />
</label>

是否需要这样做?我希望不会。

1 个答案:

答案 0 :(得分:0)

基本上,您需要在onload标记中添加一个body功能并在那里执行。

<html>
    <head>
    <script>
    function doLoad(){
       //get the the input element and set the value
       var inp = document.getElementById('filledonload');
       inp.value = yourJsFunc();
    }

    function yourJsFunc(){
       //return whaterever you want here
       return 'value on load';
    }
    </script>
    </head>
    <body onload="doLoad()">
    <form id="form1" name="formname" method="post" action="">
    <label>This Field Would Grab Text From JavaScript Function When The Page Loads
        <input type="text" name="filledonload" id="filledonload" />
    </label>
    </body>
</html>
相关问题