Hide/Show <select> depending on the other <select>

后端 未结 2 1471
天命终不由人
天命终不由人 2020-12-22 01:22

I have two

提交评论

  • 2020-12-22 01:56

    Listen to the change event for the select list. In the change event, if the selected value is "USA" or "Canada" then show the other select list, otherwise hide it.

    See an example.

    Assuming the select structure looks like this:

    <select id="countries">
        <option val="USA">USA</option>
        <option val="Canada">Canada</option>
        <option val="Australia">Australia</option>
        ...
    </select>
    
    <select id="cities">
        <option val="Vancouver">Vancouver</option>
        <option val="New York">New York</option>
        ...
    </select>
    

    Listen to the change event on the countries array.

    $("#countries").change(function() {
        // find the selected country
        var selectedCountry = $(this).val();
        // if US or Canada
        if(selectedCountry == "USA" || selectedCountry == "Canada") {
            // show the cities list
            $("#cities").show();
        }
        // otherwise hide it
        else {
            $("#cities").hide();
        }
    });
    
    0 讨论(0)
  • 提交回复
    热议问题