How to Remove duplicate dropdown option elements with same value

后端 未结 8 1074
孤城傲影
孤城傲影 2020-12-03 18:48

How can I remove duplicate values -> drop down option elements?
I have the following HTML:


相关标签:
8条回答
  • 2020-12-03 19:27

    # This is other quick option iterating options, counting the duplicates and remove when are more than one#

    var opt = $("select[title='Country'] option");              
    $.each(opt, function (key, value) {                 
      var len = $("select[title='Country'] option:contains(" + value.text + ")").length;
      if (len > 1)                   
         {
         value.remove();
         }   
      });  
    
    0 讨论(0)
  • 2020-12-03 19:37

    Not tested, but something like this should work

    var values = new Array();
    $('#YourSelect').children('option').each(function() {
      var text = $(this).text();
      if (values.indexOf(text) === -1) {
        values.push(text);
      } else {
        //  Its a duplicate
        $(this).remove()
      }
    }
    
    0 讨论(0)
提交回复
热议问题