Get selected value of option with multiple dropdown menus using javascript

限于喜欢 提交于 2019-12-12 16:40:33

问题


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.

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

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

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.

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

回答1:


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

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:


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.

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



来源:https://stackoverflow.com/questions/28729155/get-selected-value-of-option-with-multiple-dropdown-menus-using-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!