简体   繁体   English

使用 jquery 从多个下拉列表中删除重复项

[英]Remove duplicates from more than one dropdown list using jquery

I have a lot of dropdown lists in my HTML code that it gets its data from MySQL query I am using the distinct method but some duplicated text is still exist in it我的 HTML 代码中有很多下拉列表,它从 MySQL 查询中获取数据 我使用的是 distinct 方法,但其中仍然存在一些重复的文本

this is my code这是我的代码

 var code = {}; $("select[name='get'] > option").each(function() { if (code[this.text]) { $(this).remove(); } else { code[this.text] = this.value; } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="row"> <div class="col-12 col-sm-6 col-lg-3"> <label for="users-list-role">Country</label> <fieldset class="form-group"> <select class="form-control select2" name="country"> <option value="">All</option> <option value="user">User</option> <option value="staff">Staff</option> </select> </fieldset> </div> <div class="col-12 col-sm-6 col-lg-3"> <label for="users-list-status">Status</label> <fieldset class="form-group"> <select class="form-control select2" name="status"> <option value="">All</option> <option value="Active">Active</option> <option value="Blocked">Blocked</option> <option value="deactivated">Deactivated</option> </select> </fieldset> </div> <div class="col-12 col-sm-6 col-lg-3"> <label for="users-list-verified">Verified</label> <fieldset class="form-group"> <select class="form-control select2" name="get"> <option value="">All</option> <option value="true">Yes</option> <option value="false">Yes</option> <option value="false">Yes</option> </select> </fieldset> </div> <div class="col-12 col-sm-6 col-lg-3"> <label for="users-list-department">Department</label> <fieldset class="form-group"> <select class="form-control select2" name="dep"> <option value="">All</option> <option value="Sales">Sales</option> <option value="Devlopment">Devlopment</option> <option value="Management">Management</option> <option value="Management">Management</option> <option value="Management">Management</option> </select> </fieldset> </div> </div>

and i have this js code to remove dublicates from only one dropdown我有这个 js 代码可以只从一个下拉列表中删除重复项

it work great but it only remove dublicates from one drop down list all i need is to remove dublicate from more than one dropdown它工作得很好,但它只从一个下拉列表中删除重复项,我需要的是从多个下拉列表中删除重复项

You can loop over all select tags and then do the same with options:您可以遍历所有选择标签,然后对选项执行相同操作:

 $("select").each(function() { var code = {}; $(this).find('option').each(function() { if (code[this.text]) { $(this).remove(); } else { code[this.text] = this.value; } }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="row"> <div class="col-12 col-sm-6 col-lg-3"> <label for="users-list-role">Country</label> <fieldset class="form-group"> <select class="form-control select2" name="country"> <option value="">All</option> <option value="user">User</option> <option value="staff">Staff</option> </select> </fieldset> </div> <div class="col-12 col-sm-6 col-lg-3"> <label for="users-list-status">Status</label> <fieldset class="form-group"> <select class="form-control select2" name="status"> <option value="">All</option> <option value="Active">Active</option> <option value="Blocked">Blocked</option> <option value="deactivated">Deactivated</option> </select> </fieldset> </div> <div class="col-12 col-sm-6 col-lg-3"> <label for="users-list-verified">Verified</label> <fieldset class="form-group"> <select class="form-control select2" name="get"> <option value="">All</option> <option value="true">Yes</option> <option value="false">Yes</option> <option value="false">Yes</option> </select> </fieldset> </div> <div class="col-12 col-sm-6 col-lg-3"> <label for="users-list-department">Department</label> <fieldset class="form-group"> <select class="form-control select2" name="dep"> <option value="">All</option> <option value="Sales">Sales</option> <option value="Devlopment">Devlopment</option> <option value="Management">Management</option> <option value="Management">Management</option> <option value="Management">Management</option> </select> </fieldset> </div> </div>

If you have other select tags on page also, the you can specify multiple names in selector:如果页面上还有其他选择标签,则可以在选择器中指定多个名称:

 $("select [name='get'], [name='dep'], [name='status']")

I think you could just remove [name='get'] restriction to match all dropdowns.我认为您可以删除[name='get']限制以匹配所有下拉列表。

Or if you want, you can excplicitly enumerate all the selects using the , jQuery selector separator.或者,如果需要,您可以使用 , jQuery 选择器分隔符明确枚举所有选择。

$("select[name='get'] > option, select[name='dep'] > option, ...")

Or even further, if you want to be more generic, then use a custom data attribute (for example data-noduplicates) on those you want to process this way, and match against that attribute.或者更进一步,如果你想要更通用,那么在你想要以这种方式处理的那些上使用自定义数据属性(例如数据节点),并与该属性匹配。

$("select[data-noduplicates] > option")

...

<select ... data-noduplicates>...</select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM