Getting a value from a list
I have a list like that:
<select size="1">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
<option value="4">Option 4</option>
<option value="5">Option 5</option>
</select>
开发者_C百科How can I get the value of this options with JavaScript (Getting the "1", "2", "3" and so on...).
if you add the id
of "list" to <select>
this should work: Example Here
document.getElementById('list').onchange = function( e) {
alert( this.value )
}
to get it directly without using this
:
document.getElementById('list').value
document.getElementsByTagName("select")[0].value;
or
mySelection = function(element){
console.log(element.value);
}
<select onchange="mySelection(this);">
精彩评论