sort items in a dropdown list without the first item

后端 未结 6 1016
粉色の甜心
粉色の甜心 2021-01-04 02:59

I have the following code to sort the items in a dropdown list:

function sortDropDownListByText(selectId) {
    $(selectId).html($(selectId + \" option\").so         


        
6条回答
  •  迷失自我
    2021-01-04 03:55

    If the "Please select..." option has a certain value (here called "dummyVal") associated with it, you could use this in your comparison function:

    function sortDropDownListByText(selectId, dummyVal) {
        $(selectId).html($(selectId + " option").sort(function(a, b) {
            if (a.value == dummyVal) {
                return -1;
            }
            return a.text == b.text ? 0 : a.text < b.text ? -1 : 1
        })) 
    }
    

提交回复
热议问题