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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2048762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 12:39:04  来源:igfitidea点击:

sort items in a dropdown list without the first item

jqueryselectsortingdrop-down-menu

提问by leora

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

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

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

this works fine except in cases where, in my first item, i have a **"Please select and item from the list" message . . **

这很好用,除非在我的第一个项目中,我有一个 **“请从列表中选择和项目”消息。. **

is there anyway i can sort the items in the select list and keep the "Please select entry" as the first item in the list always?

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

EDIT:

编辑:

In response to some of the answers, the "Please select item always has a value of 0"

针对一些答案,“请选择项目的值始终为 0”

回答by simplyharsh

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);              

};

is your function.

是你的功能。

回答by Daniel

Theoretically, I would approach the problem by removing the "Please select" entry, sorting the list, then append it again, once the sorting is done

从理论上讲,我会通过删除“请选择”条目,对列表进行排序,然后在排序完成后再次追加来解决该问题

回答by dxh

How about always returning -1for that item?

总是回来-1买那件东西怎么样?

$(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;
});

回答by Tom Bartel

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

如果“请选择...”选项具有与之关联的特定值(此处称为“dummyVal”),您可以在比较函数中使用它:

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
    })) 
}

回答by rahul

Remove the first item from the select box and then append it at the first position after the sort function.

从选择框中删除第一项,然后将其附加到排序功能后的第一个位置。

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

回答by Ibrahim Sana

First you have to preserve the selected value, and once you finished the sorting you re-select the preserved value.

首先,您必须保留选定的值,完成排序后,您重新选择保留的值。

#store the selected value.
var selectedVal = $(selectId).val(); 

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

#re-select the preserved value.
$(selectId).val(selectedVal);