简体   繁体   中英

Drop down list selected value for provided text in java script

如果我的下拉列表包含从1到10的10个值和这些值的从1到10的文本('One'表示1,'Two'表示2'),如何获取Java脚本中所选文本的下拉列表值? ...)。如果我要传递文字“五”,则需要选择值5。

If your dropdown is marked with an id, say 'demo', you could do something like this,

var result = document.getElementById("demo");
alert(result.value);

You would also have to add a value to each item in your drop down first,

<option value="1">One</option>
<option value="2">Two</option>

and so on.

Let's say you have following dropdown

<select id="dropdown">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>

Then to get the selected text

 $("#dropdown option:selected).Text();

To get the selected value.

$("#dropdown option: selected).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