如何获取select值

献上原生js和jquery获取select值的方法==



<div style = "margin:0 auto; width:70px; height:70px; border:1px solid blue">

<select id="test" name="" onchange = c()>
<option value="1">text1</option>
<option value="2">text2</option>
</div>
</select>


<script type="text/javascript">
var c = function (){
var myselect=document.getElementById("test");
var index=myselect.selectedIndex;
alert("被选项目的值:"+myselect.selectedIndex+",被选项目的显示值:"+ myselect.options[index].text +"。原生js方式");
}
</script>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
// 测试引用是否成功
//$(document).ready(function(e) {
//  alert('test!');
//});
$("#test").change(function() {
        a = $("#test").val();
        b = $("#test option:checked").text();
        alert("被选项目的值:"+a+",被选项目的显示值:"+b+"。jquery方式");
    });
</script>

猜你喜欢

转载自blog.csdn.net/qq_32784303/article/details/80136428