CFXIXI工作室首页
CF西西的博客 | 如何使用javascript获得select的选中值

如何使用javascript获得select的选中值

4. 七月 2013

How to get the selected value of dropdownlist using JavaScript?

参考:http://stackoverflow.com/questions/1085801/how-to-get-the-selected-value-of-dropdownlist-using-javascript

<selectid="ddlViewBy">

<optionvalue="1">test1</option>

<optionvalue="2"selected="selected">test2</option>

<optionvalue="3">test3</option></select>

Running this code:

var e = document.getElementById("ddlViewBy");

var strUser = e.options[e.selectedIndex].value;

Would make strUser be 2. If what you actually want is test2, then do this:

var e = document.getElementById("ddlViewBy");

var strUser = e.options[e.selectedIndex].text;

javascript