从JS选择列表中获取价值 [英] Get value from select list in JS

查看:96
本文介绍了从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

这篇关于从JS选择列表中获取价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆