Home > Web Front-end > JS Tutorial > 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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-16 16:58:17
Original
1082 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:
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
Latest Issues
AngularJS Select default value?
From 1970-01-01 08:00:00
0
0
0
Select option using PHP loop
From 1970-01-01 08:00:00
0
0
0
Chrome doesn't apply padding to select options
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template