Home  >  Article  >  Web Front-end  >  A simple example of JS getting the value and text value of select_javascript skills

A simple example of JS getting the value and text value of select_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:58:171025browse
Copy code The code is as follows:

var selectObj = document.getElementById('selectId');
// Add option through object
selectId.add(new Option("first","1"))
selectId.add(new Option("Second", "2"))
// Add option by id
selectId.add(new Option("Third", "3"))
selectId.add( new Option("Fourth", "4"))
// Add method through id (you can also add method through object)
selectId.onchange = function(){
// Get through object value and text
alert(selectObj.value);
alert(selectObj.options[selectObj.selectedIndex].text);
// Get value and text by id
alert(selectId.value) ;
alert(selectId.options[selectId.selectedIndex].text);
// You can also get value and text through this
alert(this.value);
alert(this.options[ this.selectedIndex].text);
};


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn