仅当验证检查返回 true 时才通过 ajax 进行 POST

发布于 2024-12-08 10:26:01 字数 2466 浏览 0 评论 0原文

我正在使用 RSV 验证器检查我的表格。想要完成以下工作:假设用户第一次打开页面。填写完所有文本输入框后,当用户第一次单击#submit_btn时,表单提交函数会触发RSV(验证器),验证器检查是否有任何错误。如果没问题,通过 ajax 发布数据,否则 RSV 在 alert() 的帮助下显示错误消息数组。此过程仅适用于第一次

BTW:RSV - 验证器。如果验证过程中没有发生错误,myoncomplete() 函数将返回 1。如果出现问题,它会发出警报。从这里得到的

我无法让它工作。请帮助我修复逻辑/代码错误。提前谢谢

我的 JS

var int=null;
var counter=0;
function myOnComplete() {

    return 1;

}

$(document).ready(function () {

 $("#add_form").RSV({

        onCompleteHandler: myOnComplete,

        rules: [

            "required,name,Page name required",

            "required,title,Page title required.",


            ]

    }); 

 $("#add_form").submit(function (e) {
        e.preventDefault();
        dataString = $("#add_form").serialize();
        $.ajax({
            type: "POST",
            url: "processor/dbadd.php",
            data: dataString,
            dataType: "json",
            success: function (result, status, xResponse) {
                //do something if ajax call is success

                int = setInterval(call, 3000);
                var message = result.msg;
                var err = result.err;
                if (message != null) {
                    $.notifyBar({
                        cls: "success",
                        html: message
                    });
                }
                if (err != null) {
                    $.notifyBar({
                        cls: "error",
                        html: err
                    });
                }
            },
            error: function (e) {
                //ajax call failed
                alert(e);
            }
        });
});
$("#submit_btn").click(function () {  

            if(counter===0){
            if(myOnComplete()===1) $('#add_form').submit();
            else return false;
            }
            else  $('#add_form').submit();
            counter++;
            }); 

            $('#autosave').click(function(){
                if($(this).is(':checked')){
                     int = setInterval(call, 3000);
                     $('#submit_btn').attr({'value':'Save&Exit'});
                }
            else{
                    $('#submit_btn').attr({'value':'Save'});
                    clearInterval(int);
                }
            });

});

function call() {
    $('#add_form').submit();
}

I'm checking my form with RSV validator. Want to get work following: Let's say user opened page for the first time. After filling all text input boxes, when user clicks #submit_btn FOR THE FIRST TIME, the form submit function fires RSV (validator), validator checks if there is any error. If all right, posts data via ajax, else RSV shows error messages array with the help of alert(). THIS PROCEDURE ONLY FOR THE FIRST TIME

BTW: RSV - validator. If no error occured during validation process the myoncomplete() function returns 1.. If something went wrong it alerts. Got from here

I can't get it work. Please help me to fix logic/code mistakes. Thx in advance

My JS

var int=null;
var counter=0;
function myOnComplete() {

    return 1;

}

$(document).ready(function () {

 $("#add_form").RSV({

        onCompleteHandler: myOnComplete,

        rules: [

            "required,name,Page name required",

            "required,title,Page title required.",


            ]

    }); 

 $("#add_form").submit(function (e) {
        e.preventDefault();
        dataString = $("#add_form").serialize();
        $.ajax({
            type: "POST",
            url: "processor/dbadd.php",
            data: dataString,
            dataType: "json",
            success: function (result, status, xResponse) {
                //do something if ajax call is success

                int = setInterval(call, 3000);
                var message = result.msg;
                var err = result.err;
                if (message != null) {
                    $.notifyBar({
                        cls: "success",
                        html: message
                    });
                }
                if (err != null) {
                    $.notifyBar({
                        cls: "error",
                        html: err
                    });
                }
            },
            error: function (e) {
                //ajax call failed
                alert(e);
            }
        });
});
$("#submit_btn").click(function () {  

            if(counter===0){
            if(myOnComplete()===1) $('#add_form').submit();
            else return false;
            }
            else  $('#add_form').submit();
            counter++;
            }); 

            $('#autosave').click(function(){
                if($(this).is(':checked')){
                     int = setInterval(call, 3000);
                     $('#submit_btn').attr({'value':'Save&Exit'});
                }
            else{
                    $('#submit_btn').attr({'value':'Save'});
                    clearInterval(int);
                }
            });

});

function call() {
    $('#add_form').submit();
}

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

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

发布评论

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

评论(2

贪恋 2024-12-15 10:26:01

查看 RSV 代码,看起来无论您附加 RSV 到什么,都有其提交反弹,以使用 .RSV.validate() 验证数据,

如下所示:

$(this).bind('submit', {currForm: this, options: options}, $(this).RSV.validate);
});

这意味着,如果您使用 .submit(),您也会调用 .RSV.validate 。

因此,一旦您验证了信息,请尝试将您的提交绑定到标准提交功能。

编辑:帮助解释

当您使用

$("#add_form").RSV({...});

RSV javascript 代码时,将 .RSV.validate() 绑定到 add_form 元素的提交事件。这意味着当您提交 add_form 表单时,会在提交之前调用 .RSV.validate() 。

尝试在使用和不使用 .RSV() 调用的情况下运行下面的脚本代码

该脚本将记录 add_form 元素上所有事件的所有处理程序。您注意到,调用 $element.RSV({...}) 将第二个事件处理程序附加到 add_form 元素的提交事件。我不确定访问此事件处理程序以对其进行 .unbind() 的最佳方式。祝你好运 :)

jQuery.each($('#add_form').data('events'), function(i, event){
    jQuery.each(event, function(i, handler){
       console.log(handler);
    });
});

Looking through the RSV code it looks like whatever you attach RSV to has its submit rebound to validate the data using .RSV.validate()

As seen here:

$(this).bind('submit', {currForm: this, options: options}, $(this).RSV.validate);
});

Which means that if you use .submit() you are calling .RSV.validate also.

So once you validate the info try binding your submit to the standard submit function.

Edit: To help explain

When you use

$("#add_form").RSV({...});

The RSV javascript code is binding .RSV.validate() to the submit event of your add_form element. Meaning when you submit your add_form form .RSV.validate() is being called before the submit.

Try running the script code below with and without the .RSV() call

This script will log ALL handlers for ALL events on the add_form element. You notice that calling $element.RSV({...}) attaches a second event handler to the submit event of the add_form element. I am unsure of the best way to access this event handler to .unbind() it. Good luck :)

jQuery.each($('#add_form').data('events'), function(i, event){
    jQuery.each(event, function(i, handler){
       console.log(handler);
    });
});
被翻牌 2024-12-15 10:26:01

好的,据我了解,现在您只想验证第一组数据,如果验证正确,则信任用户,我通过一个简单的示例在 jsFiddle 上完成了这项工作,我想您可以使用

http://jsfiddle.net/WqnYa/9/

基本上我所做的是捕捉提交按钮点击而不是表单提交功能,也许也可以这样做。我分配一个类“needsvalidation”,当第一次验证通过时,我只需删除该类。如果该类不存在,则验证将不会初始化,因为 $(this).hasClass('needval')

如果这不是您要寻找的,那么您的问题需要更清晰:( 希望这有帮助!

OK, to my understanding now you only want to validate the first set of data and if that validates correctly trust the user, i got this working on jsFiddle with an easy example, i guess you can make use of that

http://jsfiddle.net/WqnYa/9/

Basically what i do is that i catch the submit button click and not the forms submit function, maybe it can be done that way, too. I assign a class "needsvalidation" and when ever the first validation passes, i simply remove that class. And if that class is not present, the validation will not be initialized due to $(this).hasClass('needval')

If that's not what you're looking for then your question needs way more clarity :( hope this helps though!

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