javascript Change the Dropdown values based on other dropdown

后端 未结 3 1204
悲&欢浪女
悲&欢浪女 2020-12-11 07:13

I have a requirement When selected a dropdown value, it has to filter or remove the values of other dropdown which has index that should be always greater than selected inde

相关标签:
3条回答
  • 2020-12-11 07:32

    Edit (using jQuery to get desired results):

    <select id="one">
        <option value="01:00">01:00</option>
        <option value="01:30">01:30</option>
        <option value="02:00">02:00</option>
        <option value="02:30">02:30</option>
        <option value="03:00">03:00</option>
        <option value="03:30">03:30</option>
    </select>
    
    <select id="two"></select>
    
    <script type="text/javascript">
        $(function () {
            $("#one").change(function (e) {
                $("#two").empty();
    
                var options = 
                $("#one option").filter(function(e){
                    return $(this).attr("value") > $("#one option:selected").val();
                }).clone();
    
                $("#two").append(options);
            });
        });
    </script>
    
    0 讨论(0)
  • 2020-12-11 07:43

    Vanilla JavaScript solution and demo.

    HTML

    <select name="select1" id="select1">
        <option value="">-- select an option --</option>
        <option value="01:00">01:00</option>
        <option value="01:30">01:30</option>
        <option value="02:00">02:00</option>
        <option value="02:30">02:30</option>
        <option value="03:00">03:00</option>
        <option value="03:30">03:30</option>
        <option value="04:00">04:00</option>
        <option value="04:30">04:30</option>
        <option value="05:00">05:00</option>
        <option value="05:30">05:30</option>
    </select>
    <select name="select2" id="select2">
    </select>
    

    JavaScript

    // this function must be wrapped inside onload event
    var select1 = document.getElementById("select1");
    var select2 = document.getElementById("select2");
    select1.onchange = function() {
        // empty select2
        while (select2.firstChild) {
            select2.removeChild(select2.firstChild);
        }
        if (select1.selectedIndex == 0) {
            return;
        }
        for (var i = select1.selectedIndex; i < select1.options.length; i++) {
            var o = document.createElement("option");
            o.value = select1.options[i].value;
            o.text = select1.options[i].text;
            select2.appendChild(o);
        }
    }
    
    0 讨论(0)
  • 2020-12-11 07:49

    There are multiple ways to achieve this, each might be good for a different useage.

    Filter by value

    @Craig has suggested it already. Give the dropdown options values. When something's selected in the first one, values lower or equal to its value are removed from the second dropdown: http://jsfiddle.net/q8mLH/

    Using an array of available pairs

    Create an array of the pairs you're interested in, then when select 1 is changed, update select2 available options based on what is defined in your array of allowed pairs: http://jsfiddle.net/AU6hq/

    AJAX + database pairs

    I'm not going to specify an example here, sorry, but the general idea is this: you need to have some tables in your database, let's say "countries" and "cities", where each country has an ID, and each city has its own unique ID plus the ID of the country where it lies. You'll want to display only the cities in the selected country, probably.

    You bind the change() event to the counties select and load the contents of the second select via an ajax call, querying the database for the cities that lie in the selected country.

    There might be more, but I don't quite feel like thinking too much today ;). Anyway, I hope this helps.

    [EDIT]

    My examples use jQuery, I hope it's not a problem.

    0 讨论(0)
提交回复
热议问题