jQuery-远程验证规则 [英] jQuery - remote validation rule

查看:68
本文介绍了jQuery-远程验证规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<script type="text/javascript">
$(document).ready(function() {
    jQuery(function($){
        $("#form").submit(function(e){
           if ($(this).valid()){
                $.ajax( {
                    type: "POST",
                    url: "http://localhost/My/modules/validate.php",
                });
           }
           return false;
     })});
        $("#form").validate({
            rules: {
                user: {
                    remote: {
                        url: "http://localhost/My/modules/validate.php",
                        async: false,
                    }
                }
            }
        });
});
$.validator.setDefaults({
    debug:      true,
    success:    "valid",
});
</script>

我想创建远程验证规则.本示例正确读取ajax请求.尽管返回true/false都将得出验证错误.

I want to create remote validation rules. This example correctly reads ajax requests. Though returning true/false both evaluate to validation error.

但是,删除ajax部分(jQuery())会导致缺少ajax初始化,甚至无法处理该请求.

However, deleting ajax part (jQuery()) results in lack of ajax initialization, the request isn't even handled.

异步:false似乎是被动的,删除此结果也是一样.

async: false seems to be passive, deleting this results in the same thing.

任何人都可能不知道这里有什么问题吗?

Anyone could have any slightest idea what could be wrong here?

$(document).ready(function() {
        $("#form").validate({
            rules: {
                user: {
                    remote: "http://localhost/My/modules/validate.php",
                }
            },
            submitHandler: function (form) {
                // do your ajax form submission here
                return false; // prevent redirect since you did ajax
            }
        });
});
</script>

推荐答案

使用Validate插件时,不需要外部submit处理程序,因为它已经内置了所有事件处理程序.

You do not need an external submit handler when using the Validate plugin because it already has all the event handlers built in.

仅当表单有效时才会触发内置的submitHandler,这是您放置任何ajax进行表单提交的地方.

The built-in submitHandler is only fired whenever the form is valid and it's where you'd put any ajax for form submission.

您正在两个地方执行ajax ...使用remote规则和您的submit处理程序.

You are doing ajax in two places... with the remote rule and with your submit handler.

  • 如果您的ajax用于验证字段,请使用remote规则.
  • 如果您的Ajax用于提交表单,请使用submitHandler.
  • Use the remote rule if your ajax is used for validating a field.
  • Use submitHandler if your ajax is used for form submission.

根据文档,这仅使用 remote规则:

This is using only the remote rule, as per documentation:

$(document).ready(function () {

    $("#form").validate({
        rules: {
            user: {
                // ajax in remote rule to check this field
                remote:  "http://localhost/My/modules/validate.php"
                }
            }
        }
    });

});

// get rid of all this
//$.validator.setDefaults({
    //debug: true,  // not needed in production
    //success: "valid", // this is already the default.
//});


以下只是我的建议,如果您正在使用ajax提交表单.否则忽略它.


The following is only my suggestion if you're using ajax for form submission. Ignore it otherwise.

根据文档,这正在使用 submitHandler回调:

This is using the submitHandler callback, as per documentation:

$(document).ready(function () {

    $("#form").validate({
        // your rules & options,
        submitHandler: function (form) {
            // do your ajax form submission here
            return false; // prevent redirect since you did ajax
        }
    });

});

这篇关于jQuery-远程验证规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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