【问题标题】:Jquery Hiding Select from dropdown optionsJquery隐藏从下拉选项中选择
【发布时间】:2014-02-27 22:36:38
【问题描述】:

我正在使用 jquery 排序对下拉列表选项进行排序。如何在下拉菜单中隐藏选择项?

Here is my fiddle code

<select id="mySelect">
    <option value="">select</option>
    <option value="1">a</option>
    <option value="12">c</option>
    <option value="4">b</option>
    <option value="5">e</option>
    <option value="6">d</option>
    <option value="8">f</option>
</select>

 $('#mySelect').html($('#mySelect option').sort(function (x, y) {
                                            return $(x).text() < $(y).text() ? -1 : 1;
                                        }));

【问题讨论】:

    标签: jquery html sorting select hide


    【解决方案1】:

    see updated demo

    $(function() {
      //make a plugin for sort item you can use in fucture also for sort item 
        $.fn.sortList = function() {
        var list = $(this);
        var items = $("li", list).get();
        items.sort(function(a, b) {
            var listItem1 = $(a).text().toUpperCase();
            var listItem2 = $(b).text().toUpperCase();
            return (listItem1 < listItem2) ? -1 : 1;
        });
        $.each(items, function(i, itm) {
            list.append(itm);
        });
       }
      //plugin code end
    
      // Now call the plugin
         $('#mySelect').sortList();
    
    });
    

    【讨论】:

      【解决方案2】:

      查找和删除选项节点:

      $("#mySelect > option[value='']").remove();
      

      【讨论】:

        【解决方案3】:

        您可以通过使用 CSS(仅)选择器来做到这一点

        #mySelect option[value='']
        {
            display: none;
        }
        

        Issue:你会在第一次看到“选择”

        如果您同意,请尝试。否则;

        通过 CSS 选择器使用 jquery

        $("option[value='']").remove();
        

        或更好(如果页面中的选择很少):

        $("#mySelect option[value='']").remove();
        

        Jsfiddle

        祝你好运!

        【讨论】:

          【解决方案4】:

          您说您希望隐藏“选择”选项。你的意思是你想让它从排序中隐藏?

          另一种表达方式:对下拉列表中的所有内容进行排序,但第一个选项 Select 除外。如果是这样,请考虑:

              $(document).ready(function () {
                  var mySelect = $('#mySelect'),
                      mySelectOptions = $('option', mySelect),
                      selectOption = mySelectOptions.first();
                  mySelect.html(mySelectOptions
                          .filter(function (index) {
                              return index !== 0;
                          })
                          .sort(function (x, y) {
                              return $(x).text() < $(y).text() ? -1 : 1;
                          })
                      ).prepend(selectOption);
              });
          

          使用 .filter() 方法,您可以提取下拉列表中的所有选项,第一个选项除外(或者您可以根据其他条件进行过滤...)。缓存第一个选项,我们可以安全地对所有其他选项进行排序,并将第一个选项“Select”作为第一个子元素重新添加。

          【讨论】:

            猜你喜欢
            • 2013-09-23
            • 1970-01-01
            • 2014-07-04
            • 1970-01-01
            • 2014-10-20
            • 2018-10-14
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多