Get drop down value

2019-01-09 06:38发布

How to determine what is selected in the drop down? In Javascript.

5条回答
对你真心纯属浪费
2楼-- · 2019-01-09 07:06

Like this:

$dd = document.getElementById("yourselectelementid");
$so = $dd.options[$dd.selectedIndex];
查看更多
Lonely孤独者°
3楼-- · 2019-01-09 07:08
var dd = document.getElementById("dropdownID");
var selectedItem = dd.options[dd.selectedIndex].value;
查看更多
倾城 Initia
4楼-- · 2019-01-09 07:26

Use the value property of the <select> element. For example:

var value = document.getElementById('your_select_id').value;
alert(value);
查看更多
Evening l夕情丶
5楼-- · 2019-01-09 07:27

If your dropdown is something like this:

<select id="thedropdown">
  <option value="1">one</option>
  <option value="2">two</option>
</select>

Then you would use something like:

var a = document.getElementById("thedropdown");
alert(a.options[a.selectedIndex].value);

But a library like jQuery simplifies things:

alert($('#thedropdown').val());
查看更多
等我变得足够好
6楼-- · 2019-01-09 07:27
<select onchange = "selectChanged(this.value)">
  <item value = "1">one</item>
  <item value = "2">two</item>
</select>

and then the javascript...

function selectChanged(newvalue) {
  alert("you chose: " + newvalue);
}
查看更多
登录 后发表回答