Deleting duplicated values from html form drop-down option list

試著忘記壹切 提交于 2019-12-10 17:30:49

问题


My problem is in deleting duplicated option values. From begining, options values are unknown. When I selecting the city, then is processing ajax request and getting all available classifieds from that city. From this "city array" is automatically building drop-down list with streets. But there of course are duplicated option values. So, how I can 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>

WORKING:

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

回答1:


What I do myself is this:

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;
    }
});


来源:https://stackoverflow.com/questions/9366273/deleting-duplicated-values-from-html-form-drop-down-option-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!