如何在没有重定向的情况下提交 HTML 表单 [英] How to submit an HTML form without redirection
本文介绍了如何在没有重定向的情况下提交 HTML 表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有这样的表格,
<form action="/Car/Edit/17" id="myForm" method="post" name="myForm"> ... </form>
如何在不通过 JavaScript/jQuery 重定向到另一个视图的情况下提交它?
how can I submit it without redirecting to another view by JavaScript/jQuery?
我从 Stack Overflow 中阅读了大量答案,但所有答案都将我重定向到 POST 函数返回的视图.
I read plenty of answers from Stack Overflow, but all of them redirect me to the view returned by the POST function.
推荐答案
为了实现你想要的,你需要使用 jQuery Ajax 如下:
In order to achieve what you want, you need to use jQuery Ajax as below:
$('#myForm').submit(function(e){
e.preventDefault();
$.ajax({
url: '/Car/Edit/17/',
type: 'post',
data:$('#myForm').serialize(),
success:function(){
// Whatever you want to do after the form is successfully submitted
}
});
});
也试试这个:
function SubForm(e){
e.preventDefault();
var url = $(this).closest('form').attr('action'),
data = $(this).closest('form').serialize();
$.ajax({
url: url,
type: 'post',
data: data,
success: function(){
// Whatever you want to do after the form is successfully submitted
}
});
}
最终解决方案
这完美无缺.我从 Html.ActionLink(...)
function SubForm (){
$.ajax({
url: '/Person/Edit/@Model.Id/',
type: 'post',
data: $('#myForm').serialize(),
success: function(){
alert("worked");
}
});
}
这篇关于如何在没有重定向的情况下提交 HTML 表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文