2012-11-20 87 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. 

})。

相关问题