JQuery验证和放大器; ajax.beginform [英] jquery validate & ajax.beginform

查看:117
本文介绍了JQuery验证和放大器; ajax.beginform的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用JQuery验证插件不会停止提交提交Ajax.Beginform。有没有好办法,以确保Ajax.Beginform不会提交?

Submitting an Ajax.Beginform while using the JQuery Validate plugin doesn't stop the submission. Is there a good way to ensure the Ajax.Beginform is not submitted?

我也试着使用jQuery表格插件ajaxify我的形式。这工作一种享受。但是,我的表单是登录表单,如果用户成功登录,我想他们被重定向。我已经通过返回一个JavaScript对象Ajax.BeginForm做到了这一点,但使用JQuery.Form.js相同的code只显示Javascript的,而不是执行它。有没有一种方法来管理呢?

I've also tried using the JQuery Forms plugin to 'ajaxify' my form. This works a treat. However, my form is a login form and if the user successfully logs in, I'd like them to be redirected. I've done this in Ajax.BeginForm by returning a JavaScript object, but the same code using JQuery.Form.js simply displays the Javascript rather than executing it. Is there a way to manage this?

[按要求下面,我已经添加了一些样本code]

[as requested below, I've added some sample code]

控制器code

            if (Request.IsAjaxRequest())
            {
                return Content("/receipt/latest");
            }
            else
            {
                return RedirectToAction("latest", "receipt");
            }

我用这个返回一个字符串,这个js功能: -

I've used this to return a string to this js function:-

JS

            function manageResponse(responseText, statusText) {
                if (responseText.toString().substr(0, 1) == '/') {
                    window.location = responseText;
                }
                else {
                    $("#formResult").text(responseText);
                }
            }

我使用这些选项,以配合使用jQuery插件的形式响应。

I've used these options to tie the response with JQuery Form plugin.

            var options = {
                beforeSubmit: function() {
                    return $('#login').validate().form();
                },
                success: manageResponse
            };

感觉有点'哈克'给我,但它的工作原理。我想询问是否有更好的方法没有?

It feels a bit 'hacky' to me, but it works. I was enquiring whether there was a better way or not?

推荐答案

这似乎并没有太哈克给我,但他们我不是最优雅的家伙在世界上。我想我会做不同的唯一的事情是设置它是这样的:

It doesn't seem too hacky to me, but them I'm not the most elegant guy in the world. The only thing I think I'd do differently is set it up like this:

$('#login').validate(); // setup form to use validation plugin 

var options = {
    beforeSubmit: function() {
        return $('#login').valid(); // check form is valid
    },
    success: manageResponse
};

这篇关于JQuery验证和放大器; ajax.beginform的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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