Delete duplicate entries from a select box

前端 未结 4 1492
梦谈多话
梦谈多话 2021-01-05 17:18

How would I, using jQuery, remove the dups

        
        
4条回答
  •  清酒与你
    2021-01-05 17:56

    You can do it like this:

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

    Give it a try here, it's a simple approach, we're just keeping an array of found values, if we haven't found the value, add it to the array (.push()), if we have found the value, this one's a dupe we found earlier, .remove() it.

    This only crawls the

    提交评论

提交回复
热议问题