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

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

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

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

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

ok,现在我们可以取值了

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

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2018-09-11
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-01
  • 2021-08-07
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案