2017-04-26 73 views
1

的變量OUT我有以下搜索功能:獲取jquery.each()函數

function filter() { 
    $("table#list tr").each(function() { 
     var search = $("#search").val(); 
     var name = $(this).find("span.name").html(); 
     var email = $(this).find("span.email").html(); 
     var ref = $(this).find("span.reference").html(); 
     var match = false; 
     var count = 0; 
     if((name != undefined) && (email != undefined) && (ref != undefined)) { 
      if(name.indexOf(search) >= 0) match = true; 
      if(email.indexOf(search) >= 0) match = true; 
      if(ref.indexOf(search) >= 0) match = true; 
      if(match) { 
       $(this).removeClass("collapse"); 
       count++; 
      } else { 
       $(this).addClass("collapse"); 
      } 
     } 
    }); 
    $("#result-count").html(count + " results found."); 
} 

但是在最後一行,count是不確定的,因爲我創造了它裏面的功能。我如何獲得$ .each函數外的值?

編輯:我也剛剛意識到我正在重置循環內的計數,所以它會始終= 0!我如何正確計算結果?

+0

把'變種數= 0;'外'。每個()' – guradio

回答

1

聲明count以外的each塊。還要注意的是,你可以整理一下邏輯太過了一點:

var count = 0; 

$("table#list tr").each(function() { 
    var search = $("#search").val(); 
    var name = $(this).find("span.name").html(); 
    var email = $(this).find("span.email").html(); 
    var ref = $(this).find("span.reference").html(); 

    if (name == undefined || email == undefined || ref == undefined) 
    return; 

    if (name.indexOf(search) >= 0 || email.indexOf(search) >= 0 || ref.indexOf(search) >= 0) { 
    $(this).removeClass("collapse"); 
    count++; 
    } else { 
    $(this).addClass("collapse"); 
    } 
}); 

$("#result-count").html(count + " results found."); 
1

正如你所說的,只是它從每個循環:

function filter() { 
    var count =0; // put it here 
$("table#list tr").each(function() { 
    var search = $("#search").val(); 
    var name = $(this).find("span.name").html(); 
    var email = $(this).find("span.email").html(); 
    var ref = $(this).find("span.reference").html(); 
    var match = false; 
    if((name != undefined) && (email != undefined) && (ref != undefined)) { 
     if(name.indexOf(search) >= 0) match = true; 
     if(email.indexOf(search) >= 0) match = true; 
     if(ref.indexOf(search) >= 0) match = true; 
     if(match) { 
      $(this).removeClass("collapse"); 
      count++; 
     } else { 
      $(this).addClass("collapse"); 
     } 
    } 
}); 
$("#result-count").html(count + " results found."); 
} 
0
function filter() { 
    var count = 0; 
    $("table#list tr").each(function() { 
     var search = $("#search").val(); 
     var name = $(this).find("span.name").html(); 
     var email = $(this).find("span.email").html(); 
     var ref = $(this).find("span.reference").html(); 
     var match = false; 
     if((name != undefined) && (email != undefined) && (ref != undefined)) { 
      if(name.indexOf(search) >= 0) match = true; 
      if(email.indexOf(search) >= 0) match = true; 
      if(ref.indexOf(search) >= 0) match = true; 
      if(match) { 
       $(this).removeClass("collapse"); 
       count++; 
      } else { 
       $(this).addClass("collapse"); 
      } 
     } 
    }); 
    $("#result-count").html(count + " results found."); 
}