Javascript jqueryif语句发出
我试图检查filename变量是否为空。我用Javascript jqueryif语句发出,javascript,jquery,Javascript,Jquery,我试图检查filename变量是否为空。我用 $.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
$.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 );
})
$。每行,函数(行索引,行){
var fileName=$.trim(字段[0]).toLowerCase();
//将输出文件名
log(文件名);
//输出-1,5,3,15,15,5,5,6,7,-1,3,5,5
log(fileName.indexOf('.html');
如果(fileName.indexOf('.html')<0 | | window.location.href.toLowerCase().indexOf(fileName)<0)//文件无效或不同,请跳过此行
{
//如果文件名为空,则跳过此循环并选中下一个循环文件名变量
返回true;
}
//上面的脚本用于检查文件名是否为空。
var$element=$(“#”+文件名);
//一点也没表现出来。
log(文件名);
})
我的“如果”声明之前的所有内容都会显示出来,但之后不会显示
感谢您的帮助。您的if语句中有“return true”。这将返回/完成您的主要功能
更改:
在if之前添加:
var is_valid = false;
如果:
is_valid = true;
这样它就不会过早退出其中的var将使该变量成为函数作用域的局部变量,并且不会泄漏内存。而不是
返回true
,只需使用返回
。在if语句中放置一个console.log()
——如果if条件为true,函数将根据代码中的注释在该点停止。在这种情况下,$.each()
的用途是什么?您没有在回调中的任何位置使用行索引
或行
。行中有什么?字段在哪里定义?\我们可以通过使回调函数返回false,在特定迭代中中断$.each()循环。返回非false与for循环中的continue语句相同;它将立即跳到下一个迭代