jQuery每个方法都不返回值

nas*_*ash 4 methods each jquery

$(document).ready(function() {

    $('#commentForm').submit(function(){

        return $('input[type=text], textarea').each(function(index){

            if($(this).attr('value') == ""){
                alert(msgHash[$(this).attr('id')]);
                return false;

            }else{

                if(!$(this).attr('value').match(validateHash[$(this).attr('id')])){
                    //Do nothing
                    alert(msgOnError[$(this).attr('id')]);
                    return false;
                }
            }
        });

        return true;
    });
});
Run Code Online (Sandbox Code Playgroud)

这里msgOnError,msgHash和msgHash是我用来获取具有特定ID的每个文本框的消息的地图.不幸的是,每个方法都不返回false来取消提交表单.我究竟做错了什么 ??我是jQuery的新手,谢谢

Dav*_*und 13

是的,这究竟是如何each运作的.因为它实际上是一个在每次迭代中调用你的匿名函数的循环,退出这些函数,也不会退出调用函数.返回真假这里,实际上是相当于 continuebreakfor循环,repsectively.

你需要设置一个布尔标志,然后返回false(break),然后返回你的boolean标志的值. each

  • 我很高兴你把事情搞定了.如果您对回复感到满意,可以勾选该答案的接受按钮,将其标记为已接受的回答.这表明其他用户知道问题已经解决. (2认同)