jQuery技术:如何显示带有排序下拉列表的页面?

我有一个选择列表

 North East South West  

现在我希望列表中的项目使用jquery以排序的方式显示。 我应该能够手动(以随机顺序)在html代码中的列表中添加项目。 但它们应按字母顺序排列在浏览器页面中。

我一直在打破我的头脑并做了很多研发。 但是无法在加载页面时找到对dropdpwnlist进行排序的方法。 请帮忙!

    这是一个使用代码的工作小提琴: http : //sebastienayotte.wordpress.com/2009/08/04/sorting-drop-down-list-with-jquery/

    码:

     function sortDropDownListByText() { // Loop for each select element on the page. $("select").each(function() { // Keep track of the selected option. var selectedValue = $(this).val(); // Sort all the options by text. I could easily sort these by val. $(this).html($("option", $(this)).sort(function(a, b) { return a.text == b.text ? 0 : a.text < b.text ? -1 : 1 })); // Select one option. $(this).val(selectedValue); }); } 

    写一个方法并调用文件就绪()

     function sortMycombo() { $("#select_zone").html($('#select_zone option').sort(function(x, y) { return $(x).text() < $(y).text() ? -1 : 1; })) $("#select_zone").get(0).selectedIndex = 0; e.preventDefault(); }); 

    你可以使用jQuery和类似的东西:

    需要了解更多jQuery教程分享如何显示带有排序下拉列表的页面?,都可以关注jQuery技术分享栏目---计算机技术网(www.ctvol.com)!

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

      以上就是jQuery教程分享如何显示带有排序下拉列表的页面?相关内容,想了解更多jQuery开发(异常处理)及jQuery教程关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/jquerytutorial/982400.html

      (0)
      上一篇 2021年12月13日
      下一篇 2021年12月13日

      精彩推荐