Javascript 如何使用post到服务器端脚本提交表单输入值
我有很多按钮,每一个都会打开它的窗体。如何获取此时打开的表单的输入值,并将其发布到服务器上,如post(“/addOrders”,valueOfinputs)?Javascript 如何使用post到服务器端脚本提交表单输入值,javascript,jquery,forms,post,Javascript,Jquery,Forms,Post,我有很多按钮,每一个都会打开它的窗体。如何获取此时打开的表单的输入值,并将其发布到服务器上,如post(“/addOrders”,valueOfinputs)? 购买 名称 电话 接近 这是一个简单的参考: // this is the id of the forms, set the form ids accordingly. $("#idForm").submit(function(e) { var url = "path/to/your/script.php"; //
购买
名称
电话
接近
这是一个简单的参考:
// this is the id of the forms, set the form ids accordingly.
$("#idForm").submit(function(e) {
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.
}
});
e.preventDefault(); // avoid to execute the actual submit of the form.
});
试试这个
$("#submit").click(function(e){
$.post("/addOrders",$("#myForm").serialize());
return null;
})
.serialize()将把所有表单元素数据放入请求中
另外,您需要为不同的表单提交按钮提供不同的id,并且您必须为每个提交按钮执行上述代码
希望这对您有用。您希望使用相同的服务器端脚本来处理所有表单提交,还是希望不同的表单提交到不同的脚本?我不太理解这个问题。我的服务器端控制器将处理一个表单的提交,并将表单中的值放入数据库。从用户将单击submit.Understand的表单开始,您可以使用ajax。请参阅下面的答案。为什么我不能那样序列化:在$('.row')。在('click','.close',函数(e){$(this.parent('form')。serialize();}中。是的,如果您的按钮具有不同的id,则可以。我没有使用按钮id,只是在onclick函数中初始化。
$("#submit").click(function(e){
$.post("/addOrders",$("#myForm").serialize());
return null;
})