2016-11-13 1 views
1

私は次のスクリプトを使用していますが、問題なく動作するためにはform action attributeが必要です。行動や提出は、あなたのAJAX形でaction attributeAjaxはデフォルトのフォームを現在のページに送信しないようにします

$(function() { 
    var form = $('#editRes'); 
    var formMessages = $('#formMsg'); 
    // Set up an event listener for the contact form. 
    $(form).submit(function(e) { 
    // Stop the browser from submitting the form. 
    e.preventDefault(); 
    //do the validation here 
    if (!validateLog()) { 
     return; 
    } 
    // Serialize the form data. 
    var formData = $(form).serialize(); 
    // Submit the form using AJAX. 
    $.ajax({ 
     type: 'POST', 
     url: $(form).attr('action'), 
     data: formData 
    }).done(function(response) { 
     // Make sure that the formMessages div has the 'success' class. 
     $(formMessages).removeClass('error').addClass('success'); 
     // Set the message text. 
     $(formMessages).html(response); // < html(); 
     // Clear the form. 
     $('').val('') 
    }).fail(function(data) { 
     // Make sure that the formMessages div has the 'error' class. 
     $(formMessages).removeClass('success').addClass('error'); 
     // Set the message text. 
     var messageHtml = data.responseText !== '' ? data.responseText : 'Oops! An error occured!.'; 
     $(formMessages).html(messageHtml); // < html() 
    }); 

    }); 
    function validateLog() { 
    var valid = true; 
    //VALIDATE HERE 
    return valid; 
    } 
}) 
+0

申し訳ありませんが、私はあなたを取得しませんでした。あなたが望むものを説明していただけますか? – mrid

+0

こんにちは@mrid myスクリプトは、 '

' ''のようなフォームを持っている場合にのみ動作します。フォームフィールドで私のスクリプトは動作しません....意味がありますか? –

+0

サブミットの代わりに入力タイプボタンを使用します。そのボタンにclickイベントを登録し、$(form).submit()関数を削除します。 $(button).click()はアクション属性を削除します。 – ScanQR

答えて

0

を必要とせずに、現在のページの上に形成し、あなたはurl: $(form).attr('action')を使用しています。つまり、フォームはフォームのaction属性に送信されます。何もないので、ajaxは動作しません。

フォームがactionタグなしになりたい場合は、あなただけのAJAXのURLの一部別のオプションがwindow.location.hrefにある

$.ajax({ 
    type: 'POST', 
    url: 'page.php', 
    data: formData, 
    ... 
    ... 
}); 
+0

アクション属性は不要です。 submitを削除し、type = "button"を使用して、このボタンにclickイベントを登録します。 Ajax urlはaction属性の値になります。 – ScanQR

+0

申し訳ありません 'あなたはフォームの投稿URL(page.php)を書くことができます'あなたは拡張してください.... @TechBreak私は送信ボタンを削除することはできません –

+0

@TimothyCoetzeeなぜできないのですか?具体的な理由は何ですか?あなたの質問に応じて、現在のページにフォームを提出したいと思います。 – ScanQR

0

のフォーム提出のURL(page.php)を書くことができます。

サイドノート:form$()に再ラップする必要はありません。これはすでに2行目のjQueryオブジェクトであるため、formMessagesと同じです。あなたの提出した機能追加の終わりに

$(function() { 
    var form = $('#editRes'); // this is a jQuery object 
    var formMessages = $('#formMsg'); // this is also a jQuery object 

    // Set up an event listener for the contact form. 
    form.submit(function (e) { 
     // Stop the browser from submitting the form. 
     e.preventDefault(); 
     // do the validation here 
     if (!validateLog()) { 
      return; 
     } 
     // Submit the form using AJAX. 
     $.ajax({ 
      type: 'POST', 
      url: window.location.href, 
      data: form.serialize() 
     }).done(function(response) { 
      // Make sure that the formMessages div has the 'success' class. 
      formMessages.removeClass('error').addClass('success'); 
      // Set the message text. 
      formMessages.html(response); // < html(); 
      // Clear the form. 
      $('').val('') 
     }).fail(function(data) { 
      // Make sure that the formMessages div has the 'error' class. 
      formMessages.removeClass('success').addClass('error'); 
      // Set the message text. 
      var messageHtml = data.responseText !== '' ? data.responseText : 'Oops! An error occured!.'; 
      formMessages.html(messageHtml); // < html() 
     }); 
    }); 

    function validateLog() { 
     var valid = true; 
     //VALIDATE HERE 
     return valid; 
    } 
}); 
0

return false; 

これは離れて移動するからブラウザに防ぐことができます。

関連する問題