How to Remove duplicate dropdown option elements with same value

后端 未结 8 1075
孤城傲影
孤城傲影 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:24

    use this :

    $(document).ready(function () {
        var usedNames = {};
        $("select > option").each(function () {
            if (usedNames[this.value]) {
                $(this).remove();
            } else {
                usedNames[this.value] = this.text;
            }
        });
    });
    

    demo here : http://jsfiddle.net/aelor/aspMT/

提交回复
热议问题