1

こんにちは、私はこのフォームを含む単純な html ページを持っています:

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

[送信] ボタンを押すと、jQuery AJAX を使用して、特定のコードのデータベースにレコードがあるかどうかを確認する必要があります。はいの場合は、jQuery UI ダイアログをポップアップして、レコードの詳細を本当に表示するかどうかをユーザーに尋ねます (有料サービスであるため)。彼が確認したら、フォームを提出する必要があります。これは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;
  });

});

「詳細表示」ボタンを押しても何も起こりません。falseを返す同じ送信ハンドラーを入力しているためだと思います。フォーム送信が実行されるように解決するにはどうすればよいですか? お知らせ下さい。前もって感謝します。ヴォイテック

4

3 に答える 3

4

変化する

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

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

jQuery の onsubmit 関数をスキップします。

基本的な例: http://jsfiddle.net/4Ax6m/

于 2013-10-09T21:33:48.530 に答える
1

現在モードで実行されているため、 .ajaxtoを待つ必要があります。succeedasync

したがって、asyncオプション onを使用して無効にしますajaxドキュメントはこちら

あなたのために具体的に答えてください

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;
                }
            }
        });
    });
});

注:これは true と false を返し、サーバーへの送信プロセスを続行します。

于 2013-10-09T21:32:06.060 に答える