获取jquery.each()函数的变量OUT

时间:2017-04-26 07:27:38

标签: jquery

我有以下搜索功能:

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!如何正确计算结果?

3 个答案:

答案 0 :(得分: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 :(得分:1)

正如你所说,只需将它从每个循环中取出:

.lightbox {
    position: fixed;
    top: 0;
    left: 0;
    background-color: rgba(0,0,0,0.3);
    width: 100%;
    height: 100%;
    transform: translateY(-105%);
    -webkit-transform: translateY(-105%);
    transition: transform 0.001s, opacity 0.75s;
    -webkit-transition: transform 0.001s, opacity 0.75s;
    opacity: 0;
    display: flex;
    align-items: center;
    justify-content: center;
}

.lightbox-content {
     max-height: 80%; 
     overflow: auto; 
     position: absolute;
     margin: 0;
     background: white;
     border-radius: 10px 0 10px 0;
     padding: 10px;
     width: 50%;
}

答案 2 :(得分:0)

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.");
}