jQuery ajaxメソッドを使用してフォームを送信するためのこのコードから始めました(問題なく動作しています)
$( function() {
//bind an event handler to the submit event for the appointment form
    $("#idw-form").on("submit", function(e) {
//cache the form element for use in this function
    var $form = $( this );
//prevent the default submission of the form
    e.preventDefault();
//run an AJAX post request to your server-side script, $this.serialize() is the data from your form being added to the request
    $.post($form.attr('action'), $form.serialize(), function (responseData) {
        if (responseData == 1) {
            $.mobile.changePage( 'thank-you-dialog.html', { transition: "pop" } );
        } else {
            $.mobile.changePage( 'error-dialog.html', { transition: "pop" } );
        }
        return false;
    });
});
});
次に、jQuery 検証プラグインを使用した検証を導入したいと考えました。このコードを submitHandler コールバック関数に入れるだけでいいと思ったのですが、うまくいきませんでした。作業に何時間も費やし、firebug でデバッグし、最終的に機能するようになりました。問題を引き起こしていることがいくつかありました。
e.preventDefault();必要に慣れているをコメントアウトする必要がありました。(これを入力しているので、jQuery Mobile Framework で動作させるために必要だったので、このコード行だけを含めたのかもしれませんが、よくわかりません)。.serialize()'d'されていた変数を変更する必要がありました。このコードでフォームが送信されると、何らかの理由で、シリアル化関数は常に空の文字列を返します。
これが、最終的に機能するようになった修正済みのコードです。
submitHandler: function(form) {
    formObj = $( '#' + form.id );
    var replaceDiv = $( '#main-content' );
    //prevent the default submission of the form
    //e.preventDefault();
    //run an AJAX post request to your server-side script, $this.serialize() is the data from your form being added to the request
    $.ajax({
        type: 'POST',
        url: '/scripts/idw-mail-handler.php',
        dataType: 'json',
        data: formObj.serialize(),
        success: function(responseData, responseStatus, responseXml){
            console.log( 'responseData is: ' + responseData );
            console.log( 'responseStatus is: ' + responseStatus );
            console.log( 'responseXML is: ' + responseXml );
            replaceDiv.hide();
            if (responseData.sentStatus == 1) {
                replaceDiv.before(responseData.successMessage);
                $('html, body').animate({
                    scrollTop: 0
                }, 300);
                console.log( 'sentStatus equals: ' + responseData.sentStatus );
            } else {
                replaceDiv.before(responseData.errorMessage);
                $('html, body').animate({
                    scrollTop: 0
                }, 300);
                console.log( 'sentStatus equals something other than 1: ' + responseData.sentStatus );
            }
        },
        error: function(errorXml, errorStatus, errorError){
            console.log( 'ErrorXml is: ' + errorXml );
            console.log( 'errorStatus is: ' + errorStatus );
            console.log( 'errorError is: ' + errorError );
        },
        complete: function(completeXml, completeStatus){
            console.log( 'completeXML is: ' + completeXml );
            console.log( 'completeStatus is: ' + completeStatus );
        }
    });
}
もちろん、error:andcomplete:メソッド (「メソッド」は正しい用語ですか?) は必要ありません。jQueryのajax関数をよりよく理解するために使用していたものです。
そこで、質問が 3 つあります。
- jQuery 検証スクリプトには、
default防止する必要があるアクションがありますか? 動作している私のコードに基づいていないように見えることはわかっていますが、誰かがそれについて追加の洞察を持っているかどうかを確認したかった. serialize()'d'されていた変数を変更する必要があったのはなぜですか?.on("submit", function(e) { ...$(this) を実行できるようにする jQuery の発火方法を使用する場合と、jQuery 検証関数serialize()の方法を介して ajax 送信を発火する場合の違いは何ですか?submitHandler:- これを行うためのより効率的/より良い方法はありますか?
 
フィードバックをお寄せいただきありがとうございます。