Sorting dropdown list using Javascript

前端 未结 7 971
孤城傲影
孤城傲影 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

    DEPENDS ON JQUERY

    function SortOptions(id) {
        var prePrepend = "#";
        if (id.match("^#") == "#") prePrepend = "";
        $(prePrepend + id).html($(prePrepend + id + " option").sort(
            function (a, b) { return a.text == b.text ? 0 : a.text < b.text ? -1 : 1 })
        );
    }
    

    Examples:

    提交评论

提交回复
热议问题