• JavaScript实现下拉框排序


    方式一

    function sortMe(oSel){
    if (oSel==null) return;
    var ln = oSel.options.length;
    var arr = new Array(); // 这是关键部分
    var tempvalue=oSel.options[oSel.selectedIndex].value;

    // 将select中的所有option的value值将保存在Array中
    for (var i = 0; i < ln; i++)
    {

      // 如果需要对option中的文本排序,可以改为arr[i] = oSel.options[i].text;
      arr[i] =Number(oSel.options[i].value);

    }

    arr.sort(function(a,b){return a-b;}); // 开始排序

    // 清空Select中全部Option

    while (ln--)
    {
     
         oSel.options[ln] = null;
     
    }

    // 将排序后的数组重新添加到Select中

    if(tempvalue!="")
    oSel.add(new Option(tempvalue,tempvalue));
    for (i = 0; i < arr.length; i++)
    {
     
       oSel.add (new Option(arr[i], arr[i]));

    }

    }

    方式二

         function selectBySortLetter(Obj)
         {
         var nav = navigator.appVersion;
                if (nav.indexOf('MSIE')!=-1)
         {
      var keyCode = event.keyCode;
                 var realKey = String.fromCharCode(keyCode);
                 var ObjOptionLen = Obj.options.length;
                 var MatchTimes = 0;
                 var MatchOptionIndexs = "";
                 for (var i=0;i<ObjOptionLen;i++)
      {
                     if (Obj.options[i].SortLetter == realKey)
                     {
                          MatchTimes = MatchTimes + 1;
                          MatchOptionIndexs = MatchOptionIndexs + i +",";
                     }
                 }
                 if (MatchTimes>0)
                 {
                     if (MatchTimes>keyDownTimes)
                     {
                          var MatchOptionArray = new Array (MatchTimes);
                          MatchOptionArray = MatchOptionIndexs.substr(0,MatchOptionIndexs.lastIndexOf(',')).split(',');
                          Obj.options[MatchOptionArray[keyDownTimes]].selected = true;
                          keyDownTimes = keyDownTimes + 1;
                     }
                     else
                     {
                          keyDownTimes = 0;
                     }
                 }   
                }
         }

  • 相关阅读:
    程序员是怎么炼成的---OC题集--练习答案与题目(3)
    程序员是怎么炼成的---OC题集--练习答案与题目(2)
    152. Maximum Product Subarray
    151. Reverse Words in a String
    150. Evaluate Reverse Polish Notation
    148. Sort List
    147. Insertion Sort List
    145. Binary Tree Postorder Traversal
    144. Binary Tree Preorder Traversal
    140. Word Break II
  • 原文地址:https://www.cnblogs.com/interdrp/p/1434309.html
Copyright © 2020-2023  润新知