如何使用 JavaScript 获取 select 标签上的选定值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17481098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 08:29:02  来源:igfitidea点击:

How to get the selected value on select tag using JavaScript?

javascripthtml-select

提问by user2545177

I have a select statement like this:

我有一个像这样的选择语句:

<select onchange="change_session()" name="select_path" id="select_path">
    <option name="sserver_id" id="select_path" selected value="{{ i.id }}">{{ default_server_id }}</option>
    {% for i in all_server_id %}
        <option name="server_id" id="select_path" value="{{ i.id }}">{{ i.session_name }}</option>                 
    {% endfor %}
    </select>

The first option show the default_idon the top and the second options lists all the options. User can change the option and process accordingly which is send to server using AJAX. Now I want to find out which server_idhas user selected using JavaScript like this:

第一个选项显示default_id在顶部,第二个选项列出所有选项。用户可以更改选项并相应地使用 AJAX 发送到服务器。现在我想找出server_id用户使用 JavaScript 选择了哪个,如下所示:

var serverId=document.getElementById('server_id');

How can I get the selected option?

我怎样才能获得选定的选项?

采纳答案by Vijay

try this..

试试这个..

<select onchange="change_session()" name="select_path" id="select_path">
    <option  selected value="{{ i.id }}">{{ default_server_id }}</option>
    {% for i in all_server_id %}
        <option  value="{{ i.id }}">{{ i.session_name }}</option>                 
    {% endfor %}
</select>

code for javascript function

javascript函数的代码

var serverId=document.getElementById('select_path').value;

回答by Seth

First, you need to remove the 'name' and 'id' attributes from your <option>tags. You cannot have duplicate ids on a single page.

首先,您需要从<option>标签中删除 'name' 和 'id' 属性。单个页面上不能有重复的 ID。

Next, you can use this to get the element by id:

接下来,您可以使用它通过 id 获取元素:

var selected = document.getElementById('select_path').value;

回答by Naresh Kumar

use this

用这个

<select id="MySelectOption"> <option value="1">test1</option> <option value="2" selected="selected">test2</option> <option value="3">test3</option> </select>

<select id="MySelectOption"> <option value="1">test1</option> <option value="2" selected="selected">test2</option> <option value="3">test3</option> </select>

javascript

javascript

<script> var e = document.getElementById("MySelectOption"); var strUser = e.options[e.selectedIndex].value; </script>

<script> var e = document.getElementById("MySelectOption"); var strUser = e.options[e.selectedIndex].value; </script>

If you would like to get the actual text from the selected option do this:
If you would like to get the actual text from the selected option do this:

<script> var e = document.getElementById("MySelectOption"); var strUser = e.options[e.selectedIndex].text; </script>

<script> var e = document.getElementById("MySelectOption"); var strUser = e.options[e.selectedIndex].text; </script>