这是使用javascript设置选择/选项框的最有效方法吗? [英] Is this most efficient way of setting a select/option box with javascript?
本文介绍了这是使用javascript设置选择/选项框的最有效方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是使用javascript设置选择/选项框的最有效方法吗?
Is this most efficient way of setting a select/option box with javascript?
<select id='sel_activity'>
<option value='CYC'>Cycle Count</option>
<option value='INV'>Inventory Count</option>
<option value='INVDROP'>Inventory Drop off</option>
<option value='INVPICK'>Inventory Pick up</option>
<option value='TRAIN'>Training</option>
</select>
<script type="text/javascript">
var select = 'CYC';
var myselect=document.getElementById("sel_activity")
for (var i=0; i<myselect.options.length; i++){
if (myselect.options[i].value == select)
{
myselect.options[i].selected = true;
}
else
{
myselect.options[i].selected = false;
}
}
</script>
推荐答案
只要您的值是唯一的,就可以使用document.getElementById("sel_activity").value='CYC'
代替
as long as your values are unique, you can use document.getElementById("sel_activity").value='CYC'
instead
这篇关于这是使用javascript设置选择/选项框的最有效方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文