如何在不重定向的情况下提交HTML表单 [英] How to submit an HTML form without redirection

查看:122
本文介绍了如何在不重定向的情况下提交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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆