简体   繁体   English

在 ajax 响应中返回 false 不起作用并且表单不会取消提交

[英]On ajax response return false doesn't work and form doesn't cancel submit

I am facing issue, ifi am using ajax call, the return false not working.. and form submitted sucessfully.. I want that when i get response 1 form don't submit.. but on ajax request response form still submitting please help me..我遇到了问题,如果我正在使用 ajax 调用,返回 false 不起作用..并且表单提交成功..我希望当我收到响应 1 表单时不要提交..但在 ajax 请求响应表单仍然提交请帮助我..

here is code:这是代码:

<form action="<?php echo base_url(); ?>do/add/review" method="post" name="dologin" id="dologinsubmitreview" onSubmit="return showpopupbox();">


function showpopupbox(){      
    var strs = $("form").serialize();
          var autocompleteURL = "<?php echo base_url(); ?>grahak/save_record_session?rnd=" + Math.random() +"&sessiondata="+ $("form").serialize();
            $.ajax({
                    url : autocompleteURL,
                    async: false,
                    cache: false,
                    method : "POST",
                    success : function(respd)
                    {
                        if(respd == 1){
                            $("#classiconpopupbx").show();
                            return false;
                        }
                        else {
                            return true;
                        }
                    }
                });
}

You need to redesign your flow.您需要重新设计流程。 Javascript is asynchronous, which means that the form is submitted LONG before the AJAX call is complete. Javascript 是异步的,这意味着表单在 AJAX 调用完成之前提交了很长时间。

Instead, use jQuery on to bind to the event, capture the event in the function, and run event.preventDefault() immediately which will stop the form from submitting.相反,使用 jQuery on绑定到事件,在函数中捕获事件,并立即运行event.preventDefault()这将阻止表单提交。 THEN run your AJAX call.然后运行你的 AJAX 调用。

In your AJAX success function, you'll need to decide what to do when it comes back "truthy".在您的 AJAX 成功函数中,您需要决定当它返回“真实”时要做什么。 Without knowing more about your desired outcome, it's impossible to advise how to handle that piece.如果不了解更多关于您想要的结果,就不可能建议如何处理那件作品。

<!-- remove the inline onsubmit script handler -->
<form action="<?php echo base_url(); ?>do/add/review" method="post" name="dologin" id="dologinsubmitreview">


// no-conflict safe document ready
jQuery(function($) {
    // Bind to the form submit here, and call event.preventDefault immediately
    $('#dologinsubmitreview').on('submit', function(event) {
        event.preventDefault();
        showPopUpBox(event);
    }

    function showpopupbox() {      
        var strs = $("form").serialize();
        var autocompleteURL = "<?php echo base_url(); ?>grahak/save_record_session?rnd=" + Math.random() +"&sessiondata="+ $("form").serialize();
        $.ajax({
            url : autocompleteURL,
            async: false,
            cache: false,
            method : "POST",
            success : function(respd) {
                if(respd == 1){
                    $("#classiconpopupbx").show();
                } else {
                    // Do what you need to do here if the AJAX is true
                }
            }
        });
    }
});

One way you can do this is to prevent the submit, always , then if your Ajax call returns true, post the form (and tell the code to allow it this time):您可以这样做的一种方法是阻止提交,始终,然后如果您的 Ajax 调用返回 true,则发布表单(并告诉代码这次允许它):

For starters, don't mix inline event handlers with jQuery.首先,不要将内联事件处理程序与 jQuery 混合使用。 The jQuery way is better: jQuery 方式更好:

// Start by not allowing submit
var allowSubmit = false;
$('form').submit(function(){
    var $form = $(this);
    // Only run the ajax if this is not a "real" submit
    if (!allowSubmit){
        // do the ajax call
        $.ajax({
           url: ...
           success: function(respd){
              if(respd == 1){
                  $("#classiconpopupbx").show();
              }
              else {
                  allowSubmit = true;
                  $form[0].submit(); // important - bypass jQuery event handler
              }
           } 
        });
    }
    // Conditionally allow the form to submit
    return allowSubmit;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM