简体   繁体   English

确认提交后如何提交表格? jQuery,AJAX

[英]How to submit a form after confirming submit? jQuery, AJAX

Good day, I have a simple html page containing this form: 美好的一天,我有一个包含以下表单的简单html页面:

<form:form method="post" action="details" modelAttribute="code">
  <form:input path="code"/>
  <br/>
  <input type="submit" value="Submit" />
</form:form>

When I press the Submit button I need to check whether there are some records in the database for given code using jQuery AJAX. 当我按下Submit按钮时,我需要使用jQuery AJAX检查数据库中是否存在给定代码的某些记录。 If yes then popup jQuery UI dialog to ask user whether he really wants to display record details (because it's a paid service). 如果是,则弹出jQuery UI对话框,询问用户是否真的要显示记录详细信息(因为这是一项付费服务​​)。 If he confirms I need to submit the form. 如果他确认我需要提交表格。 This is my script on the html page: 这是我在html页面上的脚本:

$(document).ready(function() {

  // Bind an event handler to the submit event
  $('form#code').submit( function() {

    // Check whether there are some records in the DB using AJAX
    $.ajax({
      url: 'getResultCount',
      type: 'post',
      dataType: 'html',
      data: $("form#code").serialize(),
      success: function(result) {
        if(result == 'null') {
          $('div#results').html('<p>No records found for ' + $('input#code').val() + '.</p>');
        } else {
          // At leat one record was found so ask the user
          $('#dialog-confirm').dialog({
            resizable: false,
            draggable: false,
            height: 240,
            width: 450,
            modal: true,
            buttons: {
              "Display details": function() {
                // User confirmed, submit the form
                $('form#code').submit();
              },
              Cancel: function() {
                $(this).dialog("close");
              }
            }
          });
        }
      }
    });

    return false;
  });

});

When I press "Display details" button nothing happens. 当我按下“显示详细信息”按钮时,什么也没有发生。 I think it is because I'm entering the same submit handler which returns false. 我认为这是因为我正在输入相同的提交处理程序,该处理程序返回false。 How to solve it so that form submit is executed? 如何解决它以便执行表单提交? Please advise. 请指教。 Thank you in advance. 先感谢您。 Vojtech Vojtech

Change 更改

$('form#code').submit();

to

$('form#code')[0].submit(); 

It will skip the jQuery onsubmit function. 它将跳过jQuery onsubmit函数。

Basic example: http://jsfiddle.net/4Ax6m/ 基本示例: http//jsfiddle.net/4Ax6m/

There is one simple answer: Do not use <input type="submit" ... /> . 有一个简单的答案:不要使用<input type="submit" ... />

You can instead use <button onlick="handler()">Submit</button> , where handler() is your function bound to the submit-event of the form in the above code. 您可以改用<button onlick="handler()">Submit</button> ,其中handler()是绑定到上述代码中表单提交事件的函数。 If your handler decides that the form should be submitted just submit it programmatically. 如果您的经理决定应提交表格,则以编程方式提交。 Edit: Which is actually already in your code. 编辑:这实际上已经在您的代码中。

You'd need to wait for the .ajax to succeed since it is currently running in async mode. 您需要等待.ajax succeed因为它当前正在async模式下运行。

So disable it using the async option on ajax . 因此,请使用ajax上的async选项禁用它。 Documentation Here 这里的文件

ANSWER SPECIFICALLY FOR YOU 专门为您解答

JS JS

$(document).ready(function () {
    // Bind an event handler to the submit event
    $('form#code').submit(function () {
        // Check whether there are some records in the DB using AJAX
        $.ajax({
            url: 'getResultCount',
            type: 'post',
            dataType: 'html',
            data: $("form#code").serialize(),
            async: false,
            success: function (result) {
                if (result == 'null') {
                    $('div#results').html('<p>No records found for ' + $('input#code').val() + '.</p>');

                    //No Records found, submitting!!
                    return true;
                } else {
                    // At leat one record was found so ask the user
                    $('#dialog-confirm').dialog({
                        resizable: false,
                        draggable: false,
                        height: 240,
                        width: 450,
                        modal: true,
                        buttons: {
                            "Display details": function () {
                                // User confirmed, submit the form
                                return true;
                            },
                            Cancel: function () {
                                //TODO: Don't think you need this line?
                                $(this).dialog("close");

                                //CANCEL!!!
                                return false;
                            }
                        }
                    });
                    //User skipped Dialog somehow...ignoring....DO NOT SUBMIT
                    return false;
                }
            }
        });
    });
});

Note: This will return true and false to continue the submit process to the server 注意:这将返回true和false,以继续向服务器提交过程

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

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