Sorting dropdown list using Javascript

前端 未结 7 970
孤城傲影
孤城傲影 2020-11-27 17:02

i want to sort the drop down items using javascript,can anyone tell me how to do this.

7条回答
  •  盖世英雄少女心
    2020-11-27 17:35

    You could use jQuery and something like this:

    $("#id").html($("#id option").sort(function (a, b) {
        return a.text == b.text ? 0 : a.text < b.text ? -1 : 1
    }))
    

    But it's probably better to ask why and what you're trying to accomplish.

提交回复
热议问题