javascript $.each 循环中的非法 continue 语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7028716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:19:33  来源:igfitidea点击:

illegal continue statement in javascript $.each loop

javascriptjquery

提问by BillPull

I am getting an error that this has an illegal continue statement. I have a list of words to check for form validation and the problem is it was matching some of the substrings to the reserved words so I created another array of clean words to match. If it matches a clean word continue else if it matches a reserved word alert the user

我收到一个错误,提示它有一个非法的 continue 语句。我有一个要检查表单验证的单词列表,问题是它将某些子字符串与保留字匹配,所以我创建了另一个干净单词数组来匹配。如果它匹配一个干净的词继续否则如果它匹配一个保留字提醒用户

$.each(resword,function(){
        $.each(cleanword,function(){
            if ( resword == cleanword ){
                continue;
            }
            else if ( filterName.toLowerCase().indexOf(this) != -1 ) {
                console.log("bad word");
                filterElem.css('border','2px solid red');
                window.alert("You can not include '" + this + "' in your Filter Name");
                fail = true;
            }
        });
    });

回答by James Allardice

The continuestatement is fine for normal JavaScript loops, but the jQuery eachmethod requires you to use the returnstatement instead. Return anything that's not false and it will behave as a continue. Return false, and it will behave as a break:

continue语句适用于普通的 JavaScript 循环,但 jQueryeach方法要求您改用该return语句。返回任何非 false 的内容,它将表现为continue. 返回 false,它将表现为break

$.each(cleanword,function(){
    if ( resword == cleanword ){
        return true;
    }
    else if ( filterName.toLowerCase().indexOf(this) != -1 ) {
        //...your code...
    }
});

For more information, see the jQuery docs.

有关更多信息,请参阅jQuery 文档

回答by DoctorMick

Replace the continue with

将继续替换为

return true;

回答by JaredPar

You're using continuewhich is meant for javascript forloops inside a jquery eachhandler. This won't work. The equivalent of continuein jquery eachthough is returning a non-false value.

您正在使用continuewhich 用于forjqueryeach处理程序中的javascript循环。这行不通。相当于continue在 jquery 中each虽然返回一个非假值。

if ( resword == cleanword ){
  return true;
}

回答by Tomgrohl

In jQuery.each loop you have to either return true or false to alter the loop interations:

在 jQuery.each 循环中,您必须返回 true 或 false 以更改循环交互:

We can break the $.each() loop at a particular iteration by making the callback function return false. Returning non-false is the same as a continue statement in a for loop; it will skip immediately to the next iteration.

我们可以通过使回调函数返回 false 来在特定迭代中中断 $.each() 循环。返回非 false 与 for 循环中的 continue 语句相同;它将立即跳到下一次迭代。

So you would need to do this:

所以你需要这样做:

$.each(resword,function(){
    $.each(cleanword,function(){
        if ( resword == cleanword ){
            return true;
        }
        else if ( filterName.toLowerCase().indexOf(this) != -1 ) {
            console.log("bad word");
            filterElem.css('border','2px solid red');
            window.alert("You can not include '" + this + "' in your Filter Name");
            fail = true;
        }
    });
});

回答by VISHNU Radhakrishnan

in Jquery each method we are calling a function each time we loop inside array, so continuewill not work, we need to return trueto get out from a function. only in simple loops without anonymous function we can use continue

在 Jquery 中,我们每次在数组内部循环时都会调用一个函数,因此continue无法工作,我们需要return true从函数中退出。仅在没有匿名函数的简单循环中我们可以使用continue

回答by Alex Richter

You can't use continue there. It will continue automatically anyway, just remove that - and I think it should work per your description.

你不能在那里继续使用。无论如何它都会自动继续,只需删除它 - 我认为它应该根据您的描述工作。