Removing duplicate values ​​from html form form dropdown

My problem is removing duplicate parameter values. From the beginning, the option values ​​are unknown. When I select a city, it processes the ajax request and receives all available ads from this city. From this "city array" a drop-down list with streets is automatically created. But, of course, these are duplicate parameter values. So how can I delete them?

<select name="det_pas_object_gatve" class="det_pas_object_select_css"> <option selected="selected" value="">--- Choose street ---</option> <option value="Barrow">Barrow</option> <option value="Hornets">Hornets</option> <option value="Barrow">Barrow</option> <option value="Stanley">Stanley</option> <option value="Simon">Simon</option> <option value="Barrow">Barrow</option> </select> 

WORKERS:

 var foundedinputs = []; $("select[name=det_pas_object_gatve] option").each(function() { if($.inArray(this.value, foundedinputs) != -1) $(this).remove(); foundedinputs.push(this.value); }); 
+4
source share
1 answer

What I do myself is:

 var seen = {}; jQuery('.det_pas_object_select_css').children().each(function() { var txt = jQuery(this).clone().wrap('<select>').parent().html(); if (seen[txt]) { jQuery(this).remove(); } else { seen[txt] = true; } }); 
+6
source

Source: https://habr.com/ru/post/1397478/


All Articles