如何根据已经预选的列表过滤下拉列表 [英] How to filter a dropdown list based on a already pre selected list

查看:178
本文介绍了如何根据已经预选的列表过滤下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据第一个预先选择的列表过滤下拉列表。第一个列表包含美国作为预选,要过滤的列表是第二个下拉菜单。我会很感激您如何解决这个问题。

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>

关于

Adit

推荐答案

jQuery

获取所选的国家和然后使用attribute-equals-selector找到< option> 并隐藏兄弟姐妹

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

数据 - * 属性添加到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>

演示

这篇关于如何根据已经预选的列表过滤下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆