MS MVC 表单 AJAXifying 技术 [英] MS MVC form AJAXifying techniques

查看:13
本文介绍了MS MVC 表单 AJAXifying 技术的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找最优雅的方式来对我的表单进行ajax化(使用jQuery).
你是怎么做到的?

I'm looking for most elegant way to ajaxify my forms (with jQuery).
How do you do this?

推荐答案

这是我的解决方案(我认为这是一个 渐进增强 解决方案),仅使用 jQuery,不使用任何插件:

Here is my solution (I think it is a Progressive enhancement solution), using only jQuery without any plugins:

var form = $('form#YourFormId');
$(':submit', form).click(function (event) {
    event.preventDefault();
    $.post(form.attr('action'), form.serialize(),
        function(data, status) {
            if(status == 'success') {
                // your code here
            }
        }
    );
});

更新:

如果你的 POST 响应是HTML with form",那么试试这个:

If your POST response is "HTML with form" then try this:

function ajaxifyForm(form) {
    $(':submit', form).click(function (event) {
        event.preventDefault();
        $.post(form.attr('action'), form.serialize(),
            function(data, status) {
                if(status == 'success') {
                    var newForm = $(data);
                    ajaxifyForm(newForm);
                    form.after(newForm).remove();
                }
            }
        );
    });
}

这篇关于MS MVC 表单 AJAXifying 技术的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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