我使用以下代码通过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.
});
如果我必须发送除post表单数据之外的自己的参数/值,我怎么做呢?谢谢。
发布于 2012-11-20 00:18:51
有几种方法可以做到。
或者向表单中添加一个隐藏字段,其中包含需要发送的名称和值。然后,当表单被序列化时,该字段也将被序列化。
另一种方法是在序列化的表单数据末尾添加内容。
$("#idForm").serialize() + "&foo=bar"
发布于 2012-11-20 00:18:06
您可以简单地将表单数据与自己的数据分开:
data : {
myData : 'foo',
formData : $("#idForm").serialize()
}
发布于 2012-11-20 00:19:31
可以通过追加加法字符串来序列化表单数据来做到这一点。喜欢
$(“#submitButtonId”).click(函数(){
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.
});
https://stackoverflow.com/questions/13469006
复制