在没有第一项的下拉列表中对项目进行排序

leo*_*ora 14 sorting jquery select drop-down-menu

我有以下代码来排序下拉列表中的项目:

function sortDropDownListByText(selectId) {
    $(selectId).html($(selectId + " option").sort(function(a, b) {
       return a.text == b.text ? 0 : a.text < b.text ? -1 : 1
    })) 
}
Run Code Online (Sandbox Code Playgroud)

这种方法很好,除非在我的第一个项目中,我有一个**"请从列表中选择和项目"消息..**

无论如何,我可以对选择列表中的项目进行排序,并始终将"请选择条目"作为列表中的第一项?

编辑:

在回答某些答案时,"请选择项目的值始终为0"

sim*_*rsh 32

function sortDropDownListByText(selectId) {
    var foption = $('#'+ selectId + ' option:first');
    var soptions = $('#'+ selectId + ' option:not(:first)').sort(function(a, b) {
       return a.text == b.text ? 0 : a.text < b.text ? -1 : 1
    });
    $('#' + selectId).html(soptions).prepend(foption);              

};
Run Code Online (Sandbox Code Playgroud)

是你的功能.