【问题标题】:sort items in a dropdown list without the first item对没有第一项的下拉列表中的项目进行排序
【发布时间】:2011-01-04 03:51:34
【问题描述】:

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

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

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

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

编辑:

针对部分回答,“请选择项的值始终为 0”

【问题讨论】:

    标签: jquery select sorting drop-down-menu


    【解决方案1】:
    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);              
    
    };
    

    是你的功能。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

          【解决方案4】:

          总是为该项目返回-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;
          });
          

          更动态:

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

          【讨论】:

            【解决方案5】:

            如果“请选择...”选项具有与之关联的特定值(此处称为“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
                })) 
            }
            

            【讨论】:

              【解决方案6】:

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

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

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2011-04-09
                • 2017-02-17
                • 1970-01-01
                • 2020-11-11
                • 1970-01-01
                • 2015-06-02
                • 1970-01-01
                相关资源
                最近更新 更多