0

divタグ内にフォームがありますが、フォームaction=""送信の結果を同じdivに表示したいので、この関数を作成しました:

$(document).ready(function() {
         $('#div1').delegate('form', 'submit', function() { // catch the form's submit events inside the div
        $.ajax({ // create an AJAX call...
            data: $(this).serialize(), // get the form data
            type: $(this).attr('method'), // GET or POST
            url: $(this).attr('action'), // the file to call
            success: function(response) { // on success..
                 //$('#div1').html(response); // update the DIV
                $('#div1').fadeOut('slow',function() {
                            $('#div1').html(response).fadeIn('slow');
                    });
        }
        });
        return false; // cancel original event to prevent form submitting
    });
                });

ただし、フォームを送信すると、フォームにロードするアクションがないため何も表示されません。同じ機能がアクション属性を持つ他のフォームでも機能するため、フォームに同じページのスクリプトを読み取らせるためにこのケースをどのように処理するかが私の質問です。フォームアクションが空のとき?

4

1 に答える 1

2

まず、すべてのフォームには常にアクションが必要です。無効な HTML です。

ただし、現在のページに投稿したいという質問を理解できれば、これを行うことができます

    url: $(this).attr('action') || window.location.href, // the file to call

これは、利用可能な場合は action 属性を取得し、アクションが存在しない場合は現在のウィンドウの場所の href を取得します。

于 2012-05-13T21:17:24.997 に答える