문제

I have this function:

function accentFold(inStr) {
    return inStr.replace(/([àáâãäå])|([ç])|([èéêë])|([ìíîï])|([ñ])|([òóôõöø])|([ß])|([ùúûü])|([ÿ])|([æ])/g,
                          function(str,a,c,e,i,n,o,s,u,y,ae) { 
                              if(a) return 'a'; 
                              else if(c) return 'c'; 
                              else if(e) return 'e'; 
                              else if(i) return 'i'; 
                              else if(n) return 'n';
                              else if(o) return 'o'; 
                              else if(s) return 's'; 
                              else if(u) return 'u'; 
                              else if(y) return 'y'; 
                              else if(ae) return 'ae'; 
                           });
}

function checkResults(searchString) {
    jQuery('.swiper-slide').each(function() {
        var currentElement = $(this);
        var valorname = currentElement.find("p").html();
        var valornamelatin = accentFold(valorname);
        var searchResult = valorname.match(searchString, "i");
        var searchResultLatin = valornamelatin.match(searchString, "i");

        if (searchResult != null || searchResultLatin != null){
            currentElement.show();
            countResults ++;
        } else {
            currentElement.hide();
        }
        swiperCatego01.reInit();
    });

    alert ("One Alert");
}

$(document).ready(function() {
    $('#friendsSearch').keyup(function(){
        var searchString = $(this).val();
        var countResults = "0";
        checkResults(searchString);

        alert(countResults);

        if (countResults == 0) {
            $('#nohayCoincidencias').fadeIn();
        } else {
            $('#nohayCoincidencias').hide();
            swiperCatego01.reInit();
        }
    });

})

What this function does is it searchs on a list of people for the matching names trough an input text box. The functions works properly hiding the names that doesn't contain the searched criteria, but the odd thing is that I can get anithing done after the .each() function, I'm not that good with jQUery and I'm wondering if there's something I'm doing wrong, I can't make the alert to work or anything else whatsoever.

Example in:

http://design.rankon.me/appdesign/invitetemplate.php

Edit:

It's actually working on Google Chrome but fails to work on Safari of iPhone wich is my final destination, so the solution must work on Safari on ios6 +

도움이 되었습니까?

해결책

You have declared countResults inside a keyup function, so its only declared inside that scope. So it throws error when you try to do

countResults ++;

You should declare the variable as global one above all functions:

 var countResults = "0";
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top