2014-03-25 20 views
0

我有以下javascript更改單擊輸入按鈕時選擇的值。點擊按鈕時,它將選擇項更改爲空白值。使用selectedIndex將值更改爲空白選擇

我試過.selectedIndex = '20'.selectedIndex = 20,仍然得到空白結果。

<script type="text/javascript"> 
function changeDefault(){ 
    document.getElementById('Skill_1').selectedIndex = '20';  
} 
</script> 

<select id="Skill_1" name="Skill_1"> 
<option "selected" value="0" >Zero</option> 
<option value="20" >Twenty</option> 
<option value="40" >Forty</option> 
</select> 

<input type='button' onclick='changeDefault()' value='Set Defaults'/> 
+1

怎麼樣'。價值=「20'' – Vatev

回答

3

selectedIndex是選擇<option>指數之首。這與value,該選項的值不同。它看起來像你想的<option>索引1所以

document.getElementById('Skill_1').selectedIndex = 1; 

另外,您可以使用value,這將找到的第一個<option>具有匹配設置。

document.getElementById('Skill_1').value = "20"; 

DEMO

+0

完美,巨大的幫助非常感謝! –

0

爲什麼你在按鈕上使用單引號?

而且selectedIndex是一個整數。

20將始終失敗,因爲您的選擇只有2個選項。

值20是指數0 價值40指數1

您應該使用的selectedIndex = 0

相關問題