如何通过js动态设置select中option选中

如题所述

<select name="menu" id="menu" >
   <option id="1" >111</option>  //  111 是显示给用户的信息
   <option id="2" >222</option>
   <option id="3" >333</option>
   <option id="4" >444</option>
   <option id="5" >555</option>
</select>
<script type="text/javascript">
function display(optionID){
   var all_options = document.getElementById("menu").options;
   for (i=0; i<all_options.length; i++){
      if (all_options[i].id == optionID)  // 根据option标签的ID来进行判断  测试的代码这里是两个等号
      {
         all_options[i].selected = true;
      }
   }
};
display("4");
</script>

温馨提示:答案为网友推荐,仅供参考