从JS选择列表中获取价值
问题描述:
我知道,这可能是很简单的问题,我试图寻找在网络解决方案,但我想不出它...
我有以下的C#/ ASPX code:
I know that this might be very simple question and I tried to find solutions in the web but I can't figure it... I have the following C# / ASPX code:
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函数,happeen后提交
And this javascript function that happeen after submit
function validateProfile() {
var el = document.getElementById("area").value;
alert(el);
}
我想要一些如何让在列表中选定值的数目。
我试着用code以上,但它不能正常工作。
I want some how to get the number of the selected value in the list. I tried with the code above but it doesn't work.
愿望的帮助,谢谢!
答
例如,如果您有
<select id="myselect">
<option value="1">value1</option>
<option value="2" selected="selected">value2</option>
<option value="3">value3</option>
</select>
要获得选择的选项做的:
To get selected option do:
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