2012-08-13 2 views
0

파일 이름 변수가 비어 있는지 확인하려고합니다. 나는 'if'진술이 나타나기 전에 모든 것을 나타낼 것이지만 그 후에는 나타나지 않을 것입니다.jquery if 문 문제

$.each(rows, function(rowIndex, row){ 





     var fileName = $.trim(fields[0]).toLowerCase(); 

       //will output fileName 
     console.log(fileName);    

     //out -1,5,3,15,15,5,5,6,7,-1,3,5,5 
       console.log(fileName.indexOf('.html'));  

     if(fileName.indexOf('.html') < 0 || window.location.href.toLowerCase().indexOf(fileName) < 0) //invalid or different file, skip this row 
     { 
      //if the filename is empty skip this loop and check next loop filename variable 

      return true; 
     } 

     //the above script is to check to see if the filename is empty.   

       var $element = $('#' + fileName); 

       //doesn't show anything at all. 
       console.log(fileName);  

}) 

도움 주셔서 감사합니다.

+0

대신'return true;를 사용하고,'return;'만 사용하십시오. – ahren

+2

'console.log()'를 넣는다 if 문 - if 조건이 참이면 코드의 주석에 따라 그 시점에서 함수가 멈춘다. 이 경우'$ .each()'의 목적은 무엇입니까? 당신은 당신의 콜백 어디에서나'rowIndex' 나'row'를 사용하지 않습니다. 행에 무엇이 있습니까? 'fields'는 어디에 정의되어 있습니까? – nnnnnn

답변

0

if 문에 "return true"가 있습니다. 이것은 메인 함수를 반환하거나 마칩니다.

변경 :

var is_valid = false; 

그리고 만약에 :

추가하면 이전

is_valid = true; 

이 방법은 조기에 종료되지 않습니다. 'var'는이 변수를 함수의 범위에 대해 로컬 변수로 만드는 것이며 메모리를 누설하지 않습니다.

+3

_ 콜백 함수가 false를 반환하여 특정 반복에서 $ .each() 루프를 깨뜨릴 수 있습니다. false가 아닌 값을 반환하면 for 루프의 continue 문과 동일합니다. 바로 다음 반복으로 건너 뜁니다 ._ [$ .each] (http://api.jquery.com/jQuery.each/) – Andreas