2012-11-20 92 views
1

可能重複:
submitting form and variables together through jquery發送通過Ajax的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. 
}); 

如果我必須發送自己的參數/值,而不是發佈表單數據,我該怎麼做? 謝謝。

回答

1

有可以做的幾種方法。

您可以使用需要發送的名稱和值向窗體添加隱藏字段。然後,當表單序列化時,該字段也將被序列化。

另一種方式是在序列化表單數據

$("#idForm").serialize() + "&foo=bar" 
3

你可以簡單地將表單數據從您自己的數據分開:

data : { 
    myData : 'foo', 
    formData : $("#idForm").serialize() 
} 
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. 

})。

相關問題