jquery 提交内部验证创建了太多递归

发布于 2024-09-26 17:38:50 字数 1497 浏览 2 评论 0原文

因为 jquery 对话框的工作方式,当使用“确认”对话框时,您必须立即返回 false,如果用户选择“确定”,则触发表单提交。

所以,我正在使用这段代码:

     function validoForm()
    {
//some code here...
        if (datosTdcIncompletos==true)
        {
            var $dialogTDC= $('<div></div>')
            .html("TDC information incomplete\n\rDo you want to continue?")
            .dialog({
                autoOpen: false,
                modal:true,
                title: 'Confirm',
                buttons: {
                    Ok: function() {$( this ).dialog( "close" ); $('#bookForm').submit();},
                    Cancel: function() {$( this ).dialog( "close"); return false;}
                }

            });
            $dialogTDC.dialog('open');
            return false;


        }
        $('#bookForm').submit();
    }

并且

<script type="text/javascript">
    $(document).ready(function() {

        $('#submitBtn').click(function (){ $('#bookForm').submit();});

        var options1 = {
                  target: '#bookForm', 
                  url: 'http://localhost/include/processForm.php',
                  type:'post', 
                  beforeSubmit:validoForm, 
                  success: showResponse
                  };
        $('#bookForm').ajaxForm(options1); 

    });
</script>

问题的出现是由于 Ok 按钮函数 (Ok: function() {$( this ).dialog( "close" ); $('#bookForm').submit ();},) 因为这是再次提交表单,我收到“太多递归”错误。

这该怎么办?

because the way jquery dialogs woks, when using a "confirm" dialog, you have to return false immediately, and if the user selects "Ok", then trigger the form submit.

so, I'm using this code:

     function validoForm()
    {
//some code here...
        if (datosTdcIncompletos==true)
        {
            var $dialogTDC= $('<div></div>')
            .html("TDC information incomplete\n\rDo you want to continue?")
            .dialog({
                autoOpen: false,
                modal:true,
                title: 'Confirm',
                buttons: {
                    Ok: function() {$( this ).dialog( "close" ); $('#bookForm').submit();},
                    Cancel: function() {$( this ).dialog( "close"); return false;}
                }

            });
            $dialogTDC.dialog('open');
            return false;


        }
        $('#bookForm').submit();
    }

and

<script type="text/javascript">
    $(document).ready(function() {

        $('#submitBtn').click(function (){ $('#bookForm').submit();});

        var options1 = {
                  target: '#bookForm', 
                  url: 'http://localhost/include/processForm.php',
                  type:'post', 
                  beforeSubmit:validoForm, 
                  success: showResponse
                  };
        $('#bookForm').ajaxForm(options1); 

    });
</script>

The problem arises because of the Ok button function (Ok: function() {$( this ).dialog( "close" ); $('#bookForm').submit();},) because this is submitting the form again and I get a "too much recursion" error.

how should this be done?

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

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

发布评论

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

评论(2

还不是爱你 2024-10-03 17:38:50

您的 SubmitBtn 点击处理程序应该只运行验证方法(并返回 false 以阻止默认机制)。

那么你的验证方法应该只在用户确认确定时调用提交。

您还需要删除最后一个 $('#bookForm').submit();在您的验证方法中,它会一直被调用。

Your submitBtn click handler should just run the validation method (and return false to prevent the default mechanism).

Then your validation method should only call submit when the user confirms OK.

You also need to remove the last $('#bookForm').submit(); in your validation method, that gets called all the time.

总以为 2024-10-03 17:38:50

您需要整个验证和对话作为一个实体来工作。因此,当单击提交按钮时,JavaScript 验证就会启动,并且对话框会一次性显示。然后对话作为验证过程的最后一步。

因此,在验证方法结束时,您应该返回从对话返回的值或返回 true(假设当表单不完整时对话会显示)。

这也意味着删除提交按钮上的单击事件,否则您将像您所看到的那样陷入循环。

You need the entire validation and dialogue to work as a single entity. So when the submit button is clicked, the javascript validation kicks in and the dialogue shows in one go. Then the dialogue acts as the final step in the validation process.

So at the end of your validate method you should return the value returned from the dialogue or return true (assuming the dialogue shows when the form is incomplete).

That also means removing the click event on the submit button as otherwise you'll just go around in a loop as you have seen.

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