开发者

In an ASP.NET MVC 3 view, how to handle all form submission in JavaScript?

In this ASP.NET MVC 3 view, I need to control form submission completely via JavaScript (I use jQuery). Specifically, my form submission logic contacts the server via SignalR, which is a Comet framework (an improvement on Ajax, basically).

How can I install a JavaScript handler for any type of submission of forms in the view (e.g., via clicking the submit button or pressing Enter in a field)? The handler also needs to completely disable 开发者_Go百科form submission while the server is processing.

Keep in mind that I'm doing unobtrusive client-side validation of forms in the view as well.


You could use the jQuery form plugin. Or do it manually by adding a global script to all pages:

$(function() {
    $('form').submit(function() {
        if ($(this).valid()) {
            // client validation passed => send it with AJAX
            $.ajax({
                url: this.action,
                type: this.method,
                data: $(this).serialize()
            });
        }
        return false;
    });
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜