如何使用JavaScript从下拉列表中获取所选值?我尝试了以下但它不起作用.
var sel = document.getElementById('select1');
var sv = sel.options[sel.selectedIndex].value;
alert(sv);
Run Code Online (Sandbox Code Playgroud)
Kan*_*kan 26
它对我很好.
我有以下HTML:
<div>
<select id="select1">
<option value="1">test1</option>
<option value="2" selected="selected">test2</option>
<option value="3">test3</option>
</select>
<br/>
<button onClick="GetSelectedItem('select1');">Get Selected Item</button>
</div>
Run Code Online (Sandbox Code Playgroud)
以下JavaScript:
function GetSelectedItem(el)
{
var e = document.getElementById(el);
var strSel = "The Value is: " + e.options[e.selectedIndex].value + " and text is: " + e.options[e.selectedIndex].text;
alert(strSel);
}
Run Code Online (Sandbox Code Playgroud)
看到您使用的是正确的ID.如果您在ASP.NET中使用它,则在呈现时ID会更改.
| 归档时间: |
|
| 查看次数: |
195848 次 |
| 最近记录: |