如何使用JavaScript从下拉列表中获取所选值?

<表单><select id=“ddlViewBy”><option value=“1”>test1</option><option value=“2”selected=“selected”>test2</option><option value=“3”>test3</option></选择></form>


当前回答

初学者可能希望使用NAME属性而不是ID属性访问select中的值。我们知道所有表单元素都需要名称,甚至在获得id之前。

因此,我将添加getElementsByName()解决方案,让新开发人员也能看到。

没有。表单元素的名称需要是唯一的,以便表单在发布后可以使用,但DOM可以允许多个元素共享名称。因此,如果可以,请考虑将ID添加到表单中,或者使用表单元素名称my_nth_select_named_x和my_nth_text_input_named_y显式添加ID。

使用getElementsByName的示例:

var e = document.getElementsByName("my_select_with_name_ddlViewBy")[0];
var strUser = e.options[e.selectedIndex].value;

其他回答

只需使用

$('#SelectBoxId选项:selected').text();获取列出的文本$('#SelectBoxId').val();用于获取所选索引值

对于如何实现这一点,我有一点不同的看法。我通常使用以下方法(这是一种更简单的方法,而且据我所知,它适用于所有浏览器):

<select onChange="functionToCall(this.value);" id="ddlViewBy">
  <option value="value1">Text one</option>
  <option value="value2">Text two</option>
  <option value="value3">Text three</option>
  <option value="valueN">Text N</option>
</select>

纯JavaScript:

var e = document.getElementById("elementId");
var value = e.options[e.selectedIndex].value;
var text = e.options[e.selectedIndex].text;

jQuery:

$("#elementId :selected").text(); // The text content of the selected option
$("#elementId").val(); // The value of the selected option

角度JS:(http://jsfiddle.net/qk5wwyct):

// HTML
<select ng-model="selectItem" ng-options="item as item.text for item in items">
</select>
<p>Text: {{selectItem.text}}</p>
<p>Value: {{selectItem.value}}</p>

// JavaScript
$scope.items = [{
  value: 'item_1_id',
  text: 'Item 1'
}, {
  value: 'item_2_id',
  text: 'Item 2'
}];

有一个解决方法,使用EasyUI框架及其所有插件。

您只需要在下拉菜单中添加一些EasyUI对象,该对象可以作为“桥”从输入中读取。

示例:easyui搜索框

左边是下拉列表,右边是easyui搜索框:

...
<input id="ss" class="easyui-searchbox" style="width:300px"
        data-options="  searcher:my_function,
                        prompt:'Enter value',
                        menu:'#mm'">
<div id="mm" style="width:200px">
    <div data-options="name:'1'">test1</div>
    <div data-options="name:'2'">test2</div>
</div>
...
...
<script type="text/javascript">
    function my_js_function(triggeredByButton = false){
        // normal text of the searchbox (what you entered)
        var value = $("#ss").searchbox("getValue");
        // what you chose from the drop-down menu
        var name = $("#ss").searchbox("getName");
...

注意:var名称是“1”或“2”,即“下拉列表的值”,而var值是在easyui搜索框中输入的值,如果您只想知道下拉列表的数值,则不相关。

我检查了EasyUI如何获取这个#mm名称,如果没有EasyUI的帮助,我无法找到如何获取这个名称。getName后面的jQuery:

getName:function(jq){
return $.data(jq[0],"searchbox").searchbox.find("input.textbox-value").attr("name");
}

注意,此函数的返回不是easyui搜索框的值,而是用作easyui查询框菜单参数的#mm下拉列表的名称。不知何故,EasyUI必须获得其他值,因此它必须是可能的。

如果您不想看到任何插件,请尽可能缩小它?或者在上面的链接中找到一个根本不需要表单的插件,我只是没有花时间。

Use:

<select id="Ultra" onchange="alert(this.value)">
    <option value="0">Select</option>
    <option value="8">text1</option>
    <option value="5">text2</option>
    <option value="4">text3</option>
</select>

从元素内部访问任何输入/表单字段时,都可以使用“this”关键字。这样就不需要在DOM树中查找表单,然后在表单中查找该元素。