select选项改变时获取选中的option的值

本来天真的以为直接this.value()就能去到select的值,可事实并非那么回事。

<script>
    document.getElementById('select').onchange=function(){
        console.log(this.value)  // return '';
    }
</script>

this是select下拉框对象,是一个集合,so,打印出this.options来看看

good,找到了,selectedIndex,就是这货,选中的值的索引

ok,现在我们可以取值了

 document.getElementById('select').onchange=function(){
       console.log(this.options[this.options.selectedIndex].value)
   }

 

posted @ 2016-03-23 10:09  谢小宝  阅读(13404)  评论(0编辑  收藏  举报