2014-10-26 14 views
0

我有一堆ul's,並且每個ul包含一堆input's。有問題的函數從「搜索框」中獲取一個字符串,並通過搜索每個位於ul中的input來嘗試在每個ul之內找到它。沒有搜索到的字符串的每個input都是FLAGGED。該函數計算FLAGGED input的數量,如果ul中的FLAGGED input的數量等於ul中的input的總數量,則可以確定搜索到的字符串不存在於那ul在所有,並且說ul應該給一類hide-this返回在jQuery中無法正常工作

function findString(subject){ 
    var increment = 0; 
    //If string cannot be found, highlight input 
    if ($(subject).val().indexOf("s") == -1){ 
     $(subject).css("background-color", "#ff0000"); 
     increment = 1; 
    } 
    console.log("Increment is " + increment + "!"); 
    //Return value by which to increment error count (can be 1 or 0) 
    return increment; 
} 

$(document).ready(function() { 
    //When new term is entered into searchbox 
    $('#query-input').bind('input', function(){ 

     //For each UL in table 
     $(".big-table ul:not(.big-table-headers)").each(function(i){ 

      //Count total number of inputs in UL 
      var inputCount = $(this).find("input").length; 
      var errorCount = 0; 
      console.log("input count is: " + inputCount); 

      //Then for each input in UL, try to find string 
      $(this).find("input").each(function(){ 
       //Increment errorCount based on return value of findString() 
       errorCount = errorCount + $(findString($(this))); 
      }); 
      console.log("errorCount is " + errorCount + "!"); 

      /*If the number of inputs in the UL containing string is the same as 
      the total number of inputs, then no inputs in the UL contain this string, 
      and the entire ul should be hidden*/ 
      if(errorCount == inputCount){ 
       $(this).addClass("hide-this"); 
      } 
     }); 
    }); 
}); 

findString()功能運作良好,並不會識別每個input不具備的字符串,但是當我嘗試返回一個數值(1或0),從功能的主要功能,findString()而是返回一個對象,我當然不能在任何數學運算中使用。我懷疑它與使用.each()迭代器有關,但我不確定。有沒有人有任何想法?

注意我迫使函數使用「S」的字符串用於測試目的,所以它不是實際上正在從搜索框中直接輸入,但功能正在被搜索框調用。

回答

0

請勿打電話給jQuery arount findString。它應該是:

errorCount = errorCount + findString($(this)); 

你的代碼試圖一個jQuery對象添加,而不是由findString返回數errorCount

+0

是的,這似乎已經照顧它。謝謝! – kplatinum777 2014-10-26 21:23:58