繁体   English   中英

在值上使用selectedIndex更改为空白选择

[英]Use of selectedIndex on value changes to blank select

当单击输入按钮时,我具有以下javascript来更改select的值。 单击该按钮时,会将选择更改为空白值。

我已经尝试了.selectedIndex = '20' .selectedIndex = 20 .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'/>

selectedIndex第一个选定的<option>索引 这与value ,那个选项的value不同。 看来您想要索引1<option> ,所以

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

另外,您也可以使用value进行设置,这将找到具有匹配的第一个<option>

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

演示

为什么在按钮上使用单引号?

并且selectedIndex是一个整数。

由于您的选择只有2个选项,因此20总是会失败。

值20是索引0值40是索引1

您应该使用selectedIndex = 0

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM