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)
   }
原文地址:https://www.cnblogs.com/xiexiaobao/p/5309882.html