js 获取select的值 / js动态给select赋值

[javascript]  view plain copy
  1. var t = document.getElementById("select1");   
  2.           var selectValue=t.options[t.selectedIndex].value;//获取select的值  
  3.             
  4.           var t1 = document.getElementById("select2");   
  5.             
  6.           for(i=0;i<t1.length;i++){//给select赋值  
  7.               if(selectValue==t.options[i].value){  
  8.                   t1.options[i].selected=true  
  9.               }  
  10.           }  
  11. 第二种方法,赋值是一样  
[javascript]  view plain copy
  1. <script>  
  2.     function getSelectValue(){  
  3.         var selectCount = document.getElementById("select1").options;  
  4.         for(var i = 0 ; i<selectCount.length;i++){  
  5.             if(selectCount[i].selected){  
  6.                 alert(selectCount[i].value);  
  7.             }  
  8.         }  
  9.     }  
  10. </script>  

 

[html]  view plain copy
  1. <select id="select1" name="select1">  
  2.          <option value="0">0</option>  
  3.          <option value="1">1</option>  
  4.          <option value="2">2</option>  
  5.     </select>  
  6.     <input type="button" id="button1" value="submit" onclick="getSelectValue()"/>  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值