怎样用JS给,option添加“选中”属性

  1. <html>  
  2. <head>  
  3.     <script>  
  4.     window.onload = function(){  
  5.          var opts = document.getElementById("select");  
  6.          var value = 2//这个值就是你获取的值;  
  7.     if(value!=""){  
  8.      for(var i=0;i<opts.options.length;i++){  
  9.            if(value==opts.options[i].value){  
  10.                   opts.options[i].selected = 'selected';  
  11.           alert(opts.options[i].value);  
  12.                     break;  
  13.              }  
  14.           }  
  15.          }  
  16. }  
  17. </script>  
  18. </head>  
  19. <body>  
  20. <select id="select">  
  21.  <option value="1">1231231</option>  
  22. <option value="2">12312312</option>  
  23. <option value="3">123123123</option>  
  24. </select>  
  25. </body>  
  26. </html>  
posted @ 2018-01-17 09:29  后海大鲨鱼、  阅读(4904)  评论(0编辑  收藏  举报