1

重複の可能性:
jquery を使用してフォームと変数を一緒に送信する

次のコードを使用して、ajax、jquery を介してフォーム データをサーバーに送信しています。

// this is the id of the submit button
$("#submitButtonId").click(function() {

    var url = "path/to/your/script.php"; // the script where you handle the form input.

    $.ajax({
           type: "POST",
           url: url,
           data: $("#idForm").serialize(), // serializes the form's elements.
           success: function(data)
           {
               alert(data); // show response from the php script.
           }
         });

    return false; // avoid to execute the actual submit of the form.
});

投稿フォームのデータ以外に独自のパラメーター/値を送信する必要がある場合、どうすればよいですか? ありがとう。

4

3 に答える 3

3

フォーム データを独自のデータから簡単に分離できます。

data : { 
  myData : 'foo',
  formData : $("#idForm").serialize()
}
于 2012-11-20T08:18:06.037 に答える
1

いくつかの方法があります。

送信する必要がある名前と値を含む非表示フィールドをフォームに追加します。次に、フォームがシリアル化されると、そのフィールドもシリアル化されます。

別の方法は、シリアル化されたフォームデータの最後にコンテンツを追加することです

$("#idForm").serialize() + "&foo=bar"
于 2012-11-20T08:18:51.213 に答える
0

追加の文字列を追加してフォームデータをシリアル化することで、これを行うことができます。お気に入り

$("#submitButtonId").クリック(関数() {

var url = "path/to/your/script.php"; // the script where you handle the form input.
var data = $("#idForm").serialize() + "&mystring=" + someId
$.ajax({
       type: "POST",
       url: url,
       data: data, // serializes the form's elements.
       success: function(data)
       {
           alert(data); // show response from the php script.
       }
     });

return false; // avoid to execute the actual submit of the form.

});

于 2012-11-20T08:19:31.510 に答える