sort items in a dropdown list without the first item

后端 未结 6 1028
粉色の甜心
粉色の甜心 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:54

    How about always returning -1 for that item?

    $(selectId).html($(selectId + " option").sort(function(a, b) {
       return a.text == "Please select an item from the list" ? -1 : a.text < b.text ? -1 : 1;
    });
    

    more dynamically:

    $(selectId).html($(selectId + " option").sort(function(a, b) {
       return a.text == $(selectId + 'option:first').text ? -1 : a.text < b.text ? -1 : 1;
    });
    

提交回复
热议问题