jQuery AJAX 送信フォーム 質問する

jQuery AJAX 送信フォーム 質問する

orderproductForm名前と未定義の数の入力を持つフォームがあります。

私は、jQuery.get や ajax など、Ajax を介してページを呼び出し、フォームのすべての入力を送信するようなものを実行したいと考えていますorderproductForm

一つの方法としては、次のようなことをすると思います

jQuery.get("myurl",
          {action : document.orderproductForm.action.value,
           cartproductid : document.orderproductForm.cartproductid.value,
           productid : document.orderproductForm.productid.value,
           ...

しかし、フォーム入力のすべてが正確にはわかりません。フォーム入力をすべて送信する機能などはありますか?

ベストアンサー1

これは簡単な参照です:

// this is the id of the form
$("#idForm").submit(function(e) {

    e.preventDefault(); // avoid to execute the actual submit of the form.

    var form = $(this);
    var actionUrl = form.attr('action');
    
    $.ajax({
        type: "POST",
        url: actionUrl,
        data: form.serialize(), // serializes the form's elements.
        success: function(data)
        {
          alert(data); // show response from the php script.
        }
    });
    
});

おすすめ記事