2012-02-20 166 views
4

我的问题是删除重复的选项值。 从开始,选项值是未知的。当我选择城市时,然后正在处理ajax请求并获取该城市的所有可用分类信息。从这个“城市阵列”中自动构建带有街道的下拉列表。但是当然有重复的选项值。那么,我如何删除它们?从html表单下拉选项列表中删除重复的值

<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> 

工作:

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

重复的问题:http://stackoverflow.com/questions/3227754/delete-duplicate-entries- from-a-select-box – alexsuslin 2012-02-20 18:19:55

+0

如果您有权访问它,那么在构建选择列表之前修改数据服务器端可能会更容易。 – 2012-02-20 18:22:51

+0

@Alexsuslin和Brian Glaz,谢谢。 – Vital 2012-02-20 18:39:58

回答

6

我要做我自己是这样的:

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; 
    } 
}); 
相关问题