使用javascript从多个下拉菜单中获取选项的选项值

时间:2022-12-03 07:53:27

I have multiple features that have multiple options that need to be updated when an option is selected. I also need to pass a third piece of data through the attribute element.

我有多个功能,有多个选项需要在选择选项时更新。我还需要通过attribute元素传递第三个数据。

.getElementById() works for a single dropdown menu, but how do I get it to work when there are multiple menus on the page?

.getElementById()适用于单个下拉菜单,但如果页面上有多个菜单,如何让它工作?

I have tried var e = document.getElementsByClassName("testClass"); which did not work.

我试过var e = document.getElementsByClassName(“testClass”);这没用。

I also tried to create optionsText & optionsValue in the same way that optionsFtr is created with var optionsValue = $('option:selected', this).value; and that didn't work either.

我也尝试创建optionsText和optionsValue,就像使用var optionsValue = $('option:selected',this)创建optionsFtr一样.value;那也不起作用。

http://jsfiddle.net/8awqLek4/4/

HTML Code

<ul>
    <li>
        <div class="ftrsTitle">BODY</div>
        <select class="testClass" id="testId">
            <option>Select</option>
            <option ftr="bod" value="blk">Black</option>
            <option ftr="bod" value="grn">Kelly Green</option>
            <option ftr="bod" value="red">Red</option>
            <option ftr="bod" value="roy">Royal</option>
        </select>
    </li>
    <li>
        <div class="ftrsTitle">TRIM</div>
        <select class="testClass">
            <option>Select</option>
            <option ftr="trm" value="blk">Black</option>
            <option ftr="trm" value="grn">Kelly Green</option>
            <option ftr="trm" value="red">Red</option>
            <option ftr="trm" value="roy">Royal</option>
        </select>
    </li>
</ul>
<div id="vars"></div>

Javascript Code

$(document).ready(function () {
    $("select").on('change', function () {
        var e = document.getElementById("testId");
        var optionsText = e.options[e.selectedIndex].text;
        var optionsValue = e.options[e.selectedIndex].value;
        var optionsFtr = $('option:selected', this).attr('ftr');
        $("#vars").html("<p>optionsText: " + optionsText + "</p><p>optionsValue: " + optionsValue + "</p><p>optionsFtr: " + optionsFtr + "</p>");
    });
});

2 个解决方案

#1


1  

To read select value you can simply use $(this).val(). To get selected option label you should use text() method.

要读取选择值,您只需使用$(this).val()。要获得选定的选项标签,您应该使用text()方法。

Fixed code looks like this:

固定代码如下所示:

$("select").on('change', function () {
    var optionsText = $('option:selected', this).text();
    var optionsValue = $(this).val();
    var optionsFtr = $('option:selected', this).attr('ftr');
    $("#vars").html("<p>optionsText: " + optionsText + "</p><p>optionsValue: " + optionsValue + "</p><p>optionsFtr: " + optionsFtr + "</p>");
});

Demo: http://jsfiddle.net/8awqLek4/3/

#2


0  

This should do it:

这应该这样做:

$(document).ready(function(){
    $("select").on('change', function(){
                var e = $(this).find("option:selected");
                $("#vars").html("<p>optionsText: " + e.text() + "</p><p>optionsValue: " + $(this).val() + "</p><p>optionsFtr: " + e.attr("ftr") + "</p>");
        });
});

use the this keyword. Than you can access the select targeted, and with find in combination with the :selected selector you can find the option element currently selected.

使用this关键字。您可以访问选择的目标,并与find结合使用:选择的选择器,您可以找到当前选择的选项元素。

http://jsfiddle.net/8awqLek4/5/

#1


1  

To read select value you can simply use $(this).val(). To get selected option label you should use text() method.

要读取选择值,您只需使用$(this).val()。要获得选定的选项标签,您应该使用text()方法。

Fixed code looks like this:

固定代码如下所示:

$("select").on('change', function () {
    var optionsText = $('option:selected', this).text();
    var optionsValue = $(this).val();
    var optionsFtr = $('option:selected', this).attr('ftr');
    $("#vars").html("<p>optionsText: " + optionsText + "</p><p>optionsValue: " + optionsValue + "</p><p>optionsFtr: " + optionsFtr + "</p>");
});

Demo: http://jsfiddle.net/8awqLek4/3/

#2


0  

This should do it:

这应该这样做:

$(document).ready(function(){
    $("select").on('change', function(){
                var e = $(this).find("option:selected");
                $("#vars").html("<p>optionsText: " + e.text() + "</p><p>optionsValue: " + $(this).val() + "</p><p>optionsFtr: " + e.attr("ftr") + "</p>");
        });
});

use the this keyword. Than you can access the select targeted, and with find in combination with the :selected selector you can find the option element currently selected.

使用this关键字。您可以访问选择的目标,并与find结合使用:选择的选择器,您可以找到当前选择的选项元素。

http://jsfiddle.net/8awqLek4/5/