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