2012-08-13 152 views
0

我想检查文件名变量是否为空。我使用jquery if statement issue

$.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);  

}) 

我的'if'语句之前的所有内容都会显示,但不会显示在它之后。

感谢您的帮助。

+0

而不是'return true;',只是使用'return;'。 – ahren 2012-08-13 21:45:17

+2

放置一个'console.log()'_inside_ if语句 - 如果if条件为true,则该函数根据代码中的注释停止该点。在这种情况下'$ .each()'的目的是什么?回调中的任何地方都没有使用'rowIndex'或'row'。 '行'中有什么? 'fields'定义在哪里? – nnnnnn 2012-08-13 21:49:40

回答

0

你的if语句在其中有“返回true”。这将返回/完成您的主要功能。

变化:

之前如果加:

var is_valid = false; 

,并且在有:

is_valid = true; 

这样,它不会过早退出。 'var'在这里是使这个变量局部于你的函数范围内,并且不会泄漏内存。

+3

_我们可以通过使回调函数返回false来打破特定迭代中的$ .each()循环。返回非错误与for循环中的continue语句相同;它会立即跳到下一个迭代。[$ .each](http://api.jquery.com/jQuery.each/) – Andreas 2012-08-13 21:47:52