2013-05-16 22 views
0

我想我可以找到這個網絡的解決方案,但我似乎無法找到解決的辦法。通文本從JavaScript調用中的文本字段形式在頁面加載?

我有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> 

的就是需要做到這一點?我希望不是。

回答

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> 
+0

我不能得到這個工作。這使我的事情在我的一端出現了問題。我一直在嘗試劇本整夜,沒有喜悅。 –

+0

感謝fmodos,我沒有快樂試圖在的jsfiddle。任何想法我做錯了什麼?一探究竟。 http://jsfiddle.net/alwaysfinding/gYgcQ/ –

+0

對不起,我爲yourjsfunc和腳本關閉標籤輸入了錯誤的名稱......我是電話,現在不能測試它,但它會工作 – fmodos

相關問題