jQuery的形式检查返回true,但需要返回false [英] Jquery form check returns true, but need to return false

查看:154
本文介绍了jQuery的形式检查返回true,但需要返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在这种形式的检查,当表单提交:

I am running this form check when the form is submitted:

if((formData[4].value == 1 || formData[4].value == 2) && !formData[2].value) { 
    alert('Please fill out the key field');
    return false;
} else {
    $.ajax({
        url: "/ajax/key_check.php",
        cache: false,
        data: "key=" + formData[4].value,
        dataType: "html",
        success: function(data) {
            if(data == 1) {
                alert('Key already exists');
                return false;
            }
        }
    });

    return true;
}

该脚本,它提醒键已如果数据确实== 1,但形式仍然提交存在。我想通过返回false,如果数据== 1将停止处理的形式,但它继续并增加了关键反正和弹出了键已经存在的消息。如何从数据是否== 1停止提交表单?我试着即使这样做:

The script works, it alerts key already exists if data does == 1, however the form still submits. I thought by returning false if data == 1 would stop the form from processing, however it continues and adds the key anyway and popups up key already exists message. How can I stop the form from submitting if data == 1? I tried even doing this:

if(data == 1) {
    alert('Key already exists');
    return false;
} else {
    return true;
}

然后删除返回真正的脚本的底部,但同样的问题发生。弹出大作,但形式仍然得到处理。

Then removed the return true at the bottom of the script, but the same issue happens. Pop up comes up but the form still gets processed.

推荐答案

A 返回在回调函数返回的从回调的,不是父功能像你想... ...但是你的可以的得到你想要的效果,像这样的:

A return in the callback returns from that callback, not the parent function like you want...however you can get the effect you want, like this:

if((formData[4].value == 1 || formData[4].value == 2) && !formData[2].value) { 
    alert('Please fill out the key field');
    return false;
} else {
    $.ajax({
        url: "/ajax/key_check.php",
        cache: false,
        data: "key=" + formData[4].value,
        dataType: "html",
        success: function(data) {
            if(data == 1) {
                alert('Key already exists');
            } else {
              $("#someForm")[0].submit();
            }
        }
    });
    return false;
}

这里做的事情是从来没有直接提交,但是的如果的检查正常,将调用*本地的 .submit()方法,提交表单,而不是再次运行此处理。

What this does is never submit directly, but if the check is ok, calls the *native .submit() method, submitting the form and not running this handler again.

这篇关于jQuery的形式检查返回true,但需要返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆