2015-01-08 34 views
0

我目前有一個功能,將自動滾動到啓動多選下拉列表中的活動選項。但是,我一直無法修改我的代碼,以便同時處理可變數量的下拉菜單。我的問題在於創建多個功能,或者只有一個功能可以做到這一點。創建動態數量的點擊功能

 $(document).ready(function() { 
        var $dropdowns = $('fieldset').find("button.multiselect"); //Find all the dropdowns 
        console.log($dropdowns); //Show the ones that have been found 
        $.each($dropdowns, function (i) { //Just get the index of each 
         console.log(i); //Show index 
         myFunction(i); //Create n different .click functions (each with their own globals) 
        }); 

        function myFunction(index) { 
         var not_found = true; 
         var jump_to = 0; 
         var temp = -1; 
         //Recursively find scrolling value in pixels, then move. 
         //This works for a single dropdown. 
         $('button.multiselect:eq(index)').click(function() { 
          if (not_found == true) { 
           $('.multiselect:eq(index)').dropdown('toggle'); 
           temp = $('ul.multiselect-container:eq(index) li.active').position().top; 
           $('.multiselect:eq(index)').dropdown('toggle'); 
           if (temp > jump_to) { 
            jump_to = temp; 
            not_found = false; 
           } 
           else if (temp <= jump_to) { 
            $("button.multiselect:eq(index)").click(); 
           } 
          } 
          console.log($('ul.multiselect-container:eq(index) li.active').text()); 
          $('ul.multiselect-container:eq(index)').animate({scrollTop: jump_to}, 750); //Scroll to active element 
         }) 
        } 
       }); 

我不確定我是否應該使用addEventListener()的一些技巧。你需要做的

回答

2

一件事是拿index你的串並通過變量,而不是:

$('button.multiselect:eq('+index+')') 
... 
$('.multiselect:eq('+index+')').dropdown('toggle'); 
... 

index

+0

所有實例哈哈,我覺得自己像一個傻瓜。這解決了它。乾杯。 –