开发者

Get drop down value

开发者 https://www.devze.com 2023-01-22 07:16 出处:网络
How to determine what is selected in the drop开发者_JAVA百科 down? In Javascript.If your dropdown is something like this:

How to determine what is selected in the drop开发者_JAVA百科 down? In Javascript.


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());


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

var value = document.getElementById('your_select_id').value;
alert(value);


<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);
}


var dd = document.getElementById("dropdownID");
var selectedItem = dd.options[dd.selectedIndex].value;


Like this:

$dd = document.getElementById("yourselectelementid");
$so = $dd.options[$dd.selectedIndex];
0

精彩评论

暂无评论...
验证码 换一张
取 消