在没有第一个项目的下拉列表中排序项目 [英] sort items in a dropdown list without the first item

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

问题描述

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

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

In response to some of the answers, the "Please select item always has a value of 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天全站免登陆