最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get value from select list in JS - Stack Overflow

programmeradmin5浏览0评论

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>";

And this javascript function that happeen after submit

function validateProfile() {

    var el = document.getElementById("area").value;
    alert(el);
}

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.

wish for help, thanks!

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>";

And this javascript function that happeen after submit

function validateProfile() {

    var el = document.getElementById("area").value;
    alert(el);
}

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.

wish for help, thanks!

Share Improve this question asked May 23, 2013 at 16:39 Nave TsevaNave Tseva 8789 gold badges25 silver badges47 bronze badges 4
  • I'm just curious. Is there a reason not to use DropDownList Server Control? – Win Commented May 23, 2013 at 16:45
  • Hi, Yes you right, it is better to use DropDownList Server Control but in this work there is specific request to do that like this. – Nave Tseva Commented May 23, 2013 at 16:48
  • Trivial but you could use single quotes: "<select name='Area' id='area'><option value='Default' style='display:none;'>SELECT AREA</option>"; for that string – Mark Schultheiss Commented May 23, 2013 at 16:57
  • Thanks, good advice (didn't know this befor ;), but someone knoes how to solve this?? – Nave Tseva Commented May 23, 2013 at 17:00
Add a ment  | 

3 Answers 3

Reset to default 10

For example if you have

  <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

Sample JsFiddle

Try with this

var el = document.getElementById("area");
var selectedArea = el.options[el.selectedIndex].value;

Try :

var myList = document.getElementById("area");
var selectedValue = myList.options[myList.selectedIndex].value;
var selectedText = myList.options[myList.selectedIndex].text;
发布评论

评论列表(0)

  1. 暂无评论