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)
   }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM