Home > Web Front-end > JS Tutorial > body text

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

WBOY
Release: 2016-05-16 16:58:17
Original
1024 people have browsed it
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);
};


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template