Get inner html of the selected option

前端 未结 5 1357
深忆病人
深忆病人 2020-12-15 21:30

I have something like this:

select = document.getElementById(\"select\");
select.onchange = function(){
  alert(this.value); //returns the selected value
  a         


        
5条回答
  •  悲哀的现实
    2020-12-15 22:34

    I would recommend using this snippet:

    alert(this.selectedOptions[0].text)
    

    Demo:

    The selectedOptions is an HTML collection which contains all selected options. Using this snippet gives more advantage than this.options[this.selectedIndex] when you are using multi-select. In the case of a multi-select dropdown, this.selectedOptions will contain all selected elements. You can iterate through this collection to get innerHTML of all selected items.

提交回复
热议问题