Get drop down value

*爱你&永不变心* 提交于 2019-12-17 09:45:38

问题


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


回答1:


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



回答2:


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

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



回答3:


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



回答4:


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



回答5:


Like this:

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


来源:https://stackoverflow.com/questions/4029281/get-drop-down-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!