2012-10-29 98 views
2

我在web應用程序的document.getElementById( 'articleTitle')值不工作

<script type="text/javascript"> 
    document.getElementById('articleTitle').value = "Ehsan" ; 
</script>` 

創建這些代碼,但在結果我輸入沒有價值

我輸入的是:

<input id="articleTitle" name="articleTitle" class="input-xxlarge" 
type="text" placeholder="title"> 

我的JavaScript代碼的HTML代碼

+0

嘗試把你的程序放入window.onload –

回答

6

你需要確保元素之前就存在以上您嘗試檢索它。例如,如果您的script高於HTML源代碼中的input,則腳本運行時該元素將不存在。

您可以將您的script標記移動到input的下方,該標記可以工作,也可以使用某種「DOM就緒」事件。將script標記放置在body元素的末尾,YUI teamothers建議您確保腳本低於其工作元素。

+0

非常感謝我的主人。 – Ehsan

+0

@Ehsan:不用擔心,很高興幫助。 –

2

您可以將功能的事件,舉例:

<script type="text/javascript"> 
function test(){ 
    document.getElementById('articleTitle1').value ="Ehsan";} 
</script> 
<input id="articleTitle" name="articleTitle" class="input-xxlarge" 
type="text" placeholder="title" onclick="test();"> 

我希望能幫助!

相關問題