通过jquery / ajax在表单提交中传递数据

我想使用jquery / ajax提交一个包含大约10个输入的表单,但我不知道如何通过ajax的数据参数将数据传递给它。我会序列化它们吗?

jQuery.serialize对您有所帮助。 对要提交的表单的所有字段使用name属性非常重要。 相应的代码可以是以下内容

 $("form#myFormId").submit(function() { var mydata = $("form#myFormId").serialize(); console.log(mydata); // it's only for test $.ajax({ type: "POST", url: "myUrlToPostData.php", data: mydata, success: function(response, textStatus, xhr) { console.log("success"); }, error: function(xhr, textStatus, errorThrown) { console.log("error"); } }); return false; }); 
 $.ajax({ type: "POST", url: "handle.php", data: "n="+name+"&e="+email, success: function() { alert('It worked'); } }); 

这是使用它的最简单方法。 这只会向handle.php发送两个post变量$ _POST [‘n’]和$ _POST [‘e’]。 如果你的ajax在一个名为send()的函数中,你的表单看起来像这样: