2014-11-24 24 views
0

我試圖遍歷類entry_percent中的所有滑塊值,排除當前選定的滑塊。

無論我如何嘗試它,我無法濾除當前選定的滑塊?

我失去了什麼......

下面的代碼... (註釋掉是一些我已經試過了什麼)......

// get total of sliders other then current 
     function sum_sliders() { 
      var sum=0; 
      var total=0; 

      //var sliders = $('.entry_percent:not(this)');// get every slider other then the current 

      //var sliders = $('.entry_percent').not(this); 

      //var sliders = $('.entry_percent:not(this)'); 

      //var sliders = $('.entry_percent:not(this)' 

      // var sliders = $('.entry_percent:not').(this); 

      var sliders = $('.entry_percent').not(this); 


      //iterate through each input and add to sum 
      $(sliders).each(function() { 

       sum += parseFloat(this.value); 
       console.log('Sum sliders value: '+ sum); 

      }); 

      return(sum); 

     } 
+0

你的問題完全不可理解。你的HTML看起來如何?除了$(this)之外,你的意思是什麼「(這兩件事根本沒有任何聯繫)。 – hon2a 2014-11-24 12:03:18

+1

你在哪裏使用'sum_sliders'函數? – Satpal 2014-11-24 12:03:18

+1

*「當前選擇的滑塊」*與其他*有何不同?也許有特殊的課程,例如'.active'左右?在你的例子中''這個'可能指的是任何東西,而不是一個特定的對象。 – VisioN 2014-11-24 12:03:32

回答

1

沒有this哪裏你正在尋找它。你必須通過this的功能,像這樣:

function sum_sliders() { 
    var sum=0, total=0; 
    var sliders = $('.entry_percent').not(this); 
    //iterate through each input and add to sum 
    $(sliders).each(function() { 
     sum += parseFloat(this.value); 
     console.log('Sum sliders value: '+ sum); 
    }); 
    return(sum); 
} 
$("#elementYouClickToCallFunction").click(sum_sliders); //Instead of using an anonymous function 

還有 - 從你提供的東西 - 沒有理由不只是包括sum_sliders點擊綁定匿名函數裏面的內容:

$("#elementYouClickToCallFunction").click(function(){ 
    var sum=0, total=0; 
    var sliders = $('.entry_percent').not(this); 
    //iterate through each input and add to sum 
    $(sliders).each(function() { 
     sum += parseFloat(this.value); 
     console.log('Sum sliders value: '+ sum); 
    }); 
    return(sum); 
}); 
+0

如果你這樣調用函數:'$('#elementYouClick')。click(sum_sliders)'jQuery(從內存中)將clicked-element作爲'this'傳遞給函數。 – 2014-11-24 12:13:43

+0

@DavidThomas是的,修復。 – Mooseman 2014-11-24 13:09:27

+0

我做的菜鳥錯誤。感謝啓發。 – Sage 2014-11-24 14:13:54

相關問題