在没有第一项的情况下对下拉列表中的项进行排序 [英] sort items in a dropdown list without the first item

查看:16
本文介绍了在没有第一项的情况下对下拉列表中的项进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

针对部分答案,请选择项目的值始终为 0"

推荐答案

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

};

是你的职能.

这篇关于在没有第一项的情况下对下拉列表中的项进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆