Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
667 views
in Technique[技术] by (71.8m points)

javascript - How to filter a dropdown list based on a already pre selected list

I am trying to filter the dropdown list based on the first pre-selected list. The first list is contains 'United States' as pre selection and the list to be filtered is the second drop down menu. I would appreciate inputs on how to solve this.

<ul>
    <li class="">Argentina</li>
    <li class="selected">United States</li>
    <li class="">Australia</li>
</ul>
<select data-val="false" data-val-length="Only 64 characters allowed." data-val-length-max="64" data-val-required="Country code can't be empty" id="CountryCode" name="CountryCode" class="valid">
    <option value="ARG">Argentina</option>
    <option value="USA">United States</option>
    <option value="AUS">Australia</option>
</select>

Regards,

Adit

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

jQuery

Get the selected country and then find the <option> using attribute-equals-selector and hide the siblings

$(function () {
    var country = $('.selected').data('country');
    $('#CountryCode').find('[value="' + country + '"]').siblings().hide();
    $('#CountryCode').val(country);
});

HTML

Add data-* attribute to the html elements

<ul>
    <li data-country="ARG">Argentina</li>
    <li data-country="USA" class="selected">United States</li>
    <li data-country="AUS">Australia</li>
</ul>

DEMO


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...