我知道这可能是一个非常简单的问题,我试图在网上找到解决方案,但我想不出来... 我有以下 C#/ASPX 代码:
SelectArea += "<select name=\"Area\" id=\"area\">" + "<option value=\"Default\" style=\"display:none;\">SELECT AREA</option>";
for (i = 0; i < ds.Tables[0].Rows.Count; i++)
{
AreaName = ds.Tables[0].Rows[i]["AreaName"].ToString();
SelectArea += string.Format("<option value=\"{0}\"/>{0}</option>", AreaName);
}
SelectArea += "</select>";
提交后发生的javascript函数
function validateProfile() {
var el = document.getElementById("area").value;
alert(el);
}
我想了解如何获取列表中所选值的数量。 我尝试了上面的代码,但它不起作用。
求助,谢谢!
最佳答案
例如,如果你有
<select id="myselect">
<option value="1">value1</option>
<option value="2" selected="selected">value2</option>
<option value="3">value3</option>
</select>
要获得选中的选项,请执行以下操作:
var selects = document.getElementById("myselect");
var selectedValue = selects.options[selects.selectedIndex].value;// will gives u 2
var selectedText = selects.options[selects.selectedIndex].text;// gives u value2
样本 JsFiddle
关于javascript - 从JS中的选择列表中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16719412/