使用 javascript/jQuery 对选择列表中的选项进行排序......但不是按字母顺序排列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6670041/
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-23 22:44:37  来源:igfitidea点击:

Sort Options in a Select List with javascript/jQuery.. but not alphabetically

javascriptjqueryselect

提问by glosrob

I am looking for a function that will take a list of options in a select list and sort them alphabetically but with a twist. All values with the text 'NA' should be pushed to the bottom of the list.

我正在寻找一个函数,该函数将在选择列表中获取选项列表,并按字母顺序对它们进行排序,但稍有不同。所有带有文本“NA”的值都应该被推到列表的底部。

So given a list -

所以给出一个列表 -

<select>
    <option value="1">Car</option>
    <option value="2">Bus</option>
    <option value="3">NA</option>
    <option value="4">Bike</option>
    <option value="5">Tractor</option>
    <option value="6">NA</option>
</select>

We should end up with -

我们应该以 -

<select>
    <option value="4">Bike</option>
    <option value="2">Bus</option>
    <option value="1">Car</option>
    <option value="5">Tractor</option>
    <option value="3">NA</option>
    <option value="6">NA</option>
</select>

The order of the NAs is not important.

NA 的顺序并不重要。

And don't ask whyI cannot just remove the NAs (or why there will be several options with the same texts but different underlying values, because I don't agree with it either.

并且不要问为什么我不能只删除 NA(或者为什么会有几个具有相同文本但不同基础值的选项,因为我也不同意。

回答by kei

demo: http://jsfiddle.net/4bvVz/

演示:http: //jsfiddle.net/4bvVz/

function NASort(a, b) {    
    if (a.innerHTML == 'NA') {
        return 1;   
    }
    else if (b.innerHTML == 'NA') {
        return -1;   
    }       
    return (a.innerHTML > b.innerHTML) ? 1 : -1;
};

$('select option').sort(NASort).appendTo('select');

回答by Mark Kahn

var sb = $('select');

sb.append(sb.find('option').sort(function(a, b){
    return (
        a = $(a).text(),
        b = $(b).text(),
        a == 'NA' ? 1 : b == 'NA' ? -1 : 0|a > b
    );
}));

回答by Francisco Costa

If you have more than one select in the page (multiple selects) use this:

如果页面中有多个选择(多选),请使用以下命令:

function NASort(a, b) {
    if (a.innerHTML == 'NA') {
        return 1;
    }
    else if (b.innerHTML == 'NA') {
        return -1;
    }
    return (a.innerHTML > b.innerHTML) ? 1 : -1;
};

$('select').each(function( index ) {
    $(this).find('option').sort(NASort).appendTo($(this));
});

回答by Jaime Valasek

I've made ??some changes to the original function and it worked fine.

我已经对原始函数做了一些更改,并且运行良好。

function NASort(a, b) {  
    console.log(a.innerHTML.substr(0, 1).toLowerCase() + ' > ' + b.innerHTML.substr(0, 1).toLowerCase());
    return (a.innerHTML.substr(0, 1).toLowerCase() > b.innerHTML.substr(0, 1).toLowerCase()) ? 1 : -1;
};

$('select option').sort(NASort).appendTo('select');