js 获取select的值 / js动态给select赋值【转】

var t = document.getElementById("select1");
var selectValue=t.options[t.selectedIndex].value;//获取select的值

var t1 = document.getElementById("select2");

for(i=0;i<t1.length;i++){//给select赋值
    if(selectValue==t.options[i].value){
        t1.options[i].selected=true
    }
}

第二种方法,赋值是一样

<select id="select1" name="select1">
    <option value="0">0</option>
    <option value="1">1</option>
    <option value="2">2</option> 
</select>
<input type="button" id="button1" value="submit" οnclick="getSelectValue()"/>
<script>
function getSelectValue(){
    var selectCount = document.getElementById("select1").options;
    for(var i = 0 ; i<selectCount.length;i++){
        if(selectCount[i].selected){
            alert(selectCount[i].value);
        }
    }
}
</script>

文章转自:https://blog.csdn.net/yy_2011/article/details/7885469

原文地址:https://www.cnblogs.com/KillBugMe/p/13162006.html