有效性插件jquery问题?

发布于 2024-12-06 15:09:19 字数 363 浏览 1 评论 0原文

我正在使用 有效性插件 来验证表单。它工作正常,但是如果有效性函数成功,是否可以执行回调函数,如下所示,

$("form").validity(function(){
    $("#place").require().range(3, 50);
    $("#location").require().greaterThan(4) 
}, function(){ /* Here to put the call back code */ });

因为如果表单验证成功,我想运行一些代码,任何想法将不胜感激

I'm using the validity plugin to validate a form. It's working fine but is it possible to execute a callback function if the validity function succeeded like the following

$("form").validity(function(){
    $("#place").require().range(3, 50);
    $("#location").require().greaterThan(4) 
}, function(){ /* Here to put the call back code */ });

because i want to to run some code if the form validation successes , Any idea would be appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一刻暧昧 2024-12-13 15:09:19

根据文档,这并不容易实现。

但是,您也许可以编写一个执行回调的自定义“输出模式”: http: //validity.thatscaptaintoyou.com/Demos/index.htm#CustomOutputMode

According to the docs it's not easily possible.

However, you might be able to write a custom "output mode" that executes your callback: http://validity.thatscaptaintoyou.com/Demos/index.htm#CustomOutputMode

辞别 2024-12-13 15:09:19

您可以使用 validate.start / validate.end 方法 -> http://validity.thatscaptaintoyou.com/Demos/index.htm#UsingValidityWithAjax

它看起来像这样(未经测试!):

function validateMyAjaxInputs() {
    $.validity.start();
     $("#place").require().range(3, 50);
    $("#location").require().greaterThan(4) 
    var result = $.validity.end();
    return result.valid;
}

$("form").validity(function(){

    if (validateMyAjaxInputs()) {
        // Do ajax request here
    }
});

You could use the validate.start / validate.end method -> http://validity.thatscaptaintoyou.com/Demos/index.htm#UsingValidityWithAjax

it would look something like this (untested!):

function validateMyAjaxInputs() {
    $.validity.start();
     $("#place").require().range(3, 50);
    $("#location").require().greaterThan(4) 
    var result = $.validity.end();
    return result.valid;
}

$("form").validity(function(){

    if (validateMyAjaxInputs()) {
        // Do ajax request here
    }
});
记忆之渊 2024-12-13 15:09:19

好的,有效性支持可插入的“输出模块”。

这是一个(诚然是黑客且未经测试的)将调用您的回调函数。只需将代码附加到 jquery.validity.outputs.js 或将其放入您自己的文件中,以便在有效性后加载。

(function($) {
    $.validity.outputs.callback = {
        start:function() {
            buffer = [];
        },

        end:function(results) {
            $.validity.settings.callback(results);
        },

        raise:function($obj, msg) {
            buffer.push(msg);
        },

        raiseAggregate:function($obj, msg) {
            this.raise($obj, msg);
        },

        container:function() {}
    };
})(jQuery);

用法:

$.validity.setup({
    outputMode:"callback",
    callback:function(){ /* your callback */ }
});

$("form").validity(function(){
    $("#place").require().range(3, 50);
    $("#location").require().greaterThan(4) 
});

Okay, validity supports pluggable "output modules".

Here's one (admittedly hacky and untested one) that will call your callback function. Just append the code to jquery.validity.outputs.js or put it into your own file that you load after validity.

(function($) {
    $.validity.outputs.callback = {
        start:function() {
            buffer = [];
        },

        end:function(results) {
            $.validity.settings.callback(results);
        },

        raise:function($obj, msg) {
            buffer.push(msg);
        },

        raiseAggregate:function($obj, msg) {
            this.raise($obj, msg);
        },

        container:function() {}
    };
})(jQuery);

Usage:

$.validity.setup({
    outputMode:"callback",
    callback:function(){ /* your callback */ }
});

$("form").validity(function(){
    $("#place").require().range(3, 50);
    $("#location").require().greaterThan(4) 
});
混吃等死 2024-12-13 15:09:19

通常你可以做这样的事情:

var IsValid = function(){
 $.validity.start();
 $("#place").require().range(3, 50);
 $("#location").require().greaterThan(4) 
 var result = $.validity.end();
 return result;
}

if (IsValid()) { 
 //You callBack or whatewer
}

当然使用这种模式你可以实现你的js对象,这会更方便使用。

Normally you can do this kind of stuff like this:

var IsValid = function(){
 $.validity.start();
 $("#place").require().range(3, 50);
 $("#location").require().greaterThan(4) 
 var result = $.validity.end();
 return result;
}

if (IsValid()) { 
 //You callBack or whatewer
}

Of course using this pattern you are able to implement you js object which would be more convienent to use.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文