简体   繁体   中英

How to figure out which option is chosen from a select field

Does anyone know how to retrieve the option chosen from a drop-down select box?

I'm using document.getElementById("mySelect").value and it doesn't seem to be working...

document.getElementById("mySelect").options[document.getElementById("mySelect").selectedIndex].value

Are you sure your drop-down has a value in it? Did you provide values for it?

<select id="dropdownBox">
<option value="val1">value 1</option>
<option value="val2">value 2</option>
<option value="val3">value 3</option>
<option value="val4">value 4</option>
</select>

The values are val1, val2...

It's better for code optimization to do something like:

var o = document.getElementById("mySelect");
o.options[o.selectedIndex].value;

jQuery alternative:

$("#mySelect").val();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM