禁用 为特定提交按钮启用不显眼的验证

发布于 2024-12-17 11:04:50 字数 370 浏览 1 评论 0原文

我有两个提交按钮“返回”、“继续”。当我单击“返回”时,我应该怎么做才能禁用客户端验证。我试图将 取消类 添加到按钮属性但它接缝没有帮助。

UPD。实际上,这是取消类。但如果你动态添加它(通过javascript),它似乎不起作用。

I have two submit buttons Back, Continue. What should I to do to disable client validation when I click on Back. I was trying to add cancel class to button attribute but It seams does not help.

UPD. Actually this is working cancel class. But It seams not working if you add it dynamically(by javascript).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情释 2024-12-24 11:04:50

我将一个事件处理程序附加到某些按钮,该事件处理程序更改了该特定表单上验证器对象的设置。

$(".jsCancel").click(function (e) {
    $(e.currentTarget).closest("form").validate().settings.ignore = "*"
});

这在 MVC3 中对我来说就像一种魅力。

我不知道这是否对您特别有帮助,但由于我使用 ajax 表单,因此每次替换 ajax 表单的内容时,我都必须通过使用事件 ajax success 将事件附加到这些按钮。重新解析表单并将事件附加到取消按钮的完整代码是:

$(document).ajaxSuccess(function (event, xhr, settings) {
    var $jQval = $.validator, adapters, data_validation = "unobtrusiveValidation";
    $jQval.unobtrusive.parse(document);
    $(".jsCancel").click(function (e) {
        $(e.currentTarget).closest("form").validate().settings.ignore = "*"
    });
});

I attached an event handler to certain buttons, that altered the settings of the validator object on that particular form.

$(".jsCancel").click(function (e) {
    $(e.currentTarget).closest("form").validate().settings.ignore = "*"
});

This has worked like a charm for me in MVC3.

I don't know if this helps you in particular, but since I use ajax form, I had to attach the event to these buttons each time the contents of the ajax form was replaced, by using the event ajax success. The full code that reparses the form and attaches the event to the cancel buttons is:

$(document).ajaxSuccess(function (event, xhr, settings) {
    var $jQval = $.validator, adapters, data_validation = "unobtrusiveValidation";
    $jQval.unobtrusive.parse(document);
    $(".jsCancel").click(function (e) {
        $(e.currentTarget).closest("form").validate().settings.ignore = "*"
    });
});
菊凝晚露 2024-12-24 11:04:50

使用 JavaScript 劫持按钮单击以提交表单。

这是 jQuery 的一个很好的例子:

$("#MyButton").click(function(e) { 

    //submit your form manually here
    e.preventDefault();
});

Hijack the button click for form submission using JavaScript.

Here is good example with jQuery:

$("#MyButton").click(function(e) { 

    //submit your form manually here
    e.preventDefault();
});
烟花肆意 2024-12-24 11:04:50

这实际上是对 tugberk 答案的评论,但评论并不能很好地显示代码示例。

某些浏览器版本不喜欢“preventDefault()”函数。在被这个问题困扰了几次之后,我添加了一个简单的实用函数:

//Function to prevent Default Events
function preventDefaultEvents(e)
{
    if (e.preventDefault) {
        e.preventDefault();
    } else {
        e.returnValue = false;
    }
}

您可以像这样调用它来代替“preventDefault”:

$("#CancelButton").on("click", function(event) {
    preventDefaultEvents(event);
    return false;
});

This is really a comment to the answer by tugberk, but comments don't show code examples very well.

Some browser versions do not like the "preventDefault()" function. After being bitten by this a few times, I added a simple utility function:

//Function to prevent Default Events
function preventDefaultEvents(e)
{
    if (e.preventDefault) {
        e.preventDefault();
    } else {
        e.returnValue = false;
    }
}

You call it in place of "preventDefault" like this:

$("#CancelButton").on("click", function(event) {
    preventDefaultEvents(event);
    return false;
});
爱情眠于流年 2024-12-24 11:04:50

您可以使用“取消”CSS 类。
例如:

JQuery.Validate 处理以下代码中的其余部分:

// allow suppresing validation by adding a cancel class to the submit button
this.find("input, button").filter(".cancel").click(function() {
    validator.cancelSubmit = true;
});

You can use "cancel" css class.
Ex: <input type="submit" value="Cancel" name="Cancel" class="cancel" />

JQuery.Validate handle the rest in the following code:

// allow suppresing validation by adding a cancel class to the submit button
this.find("input, button").filter(".cancel").click(function() {
    validator.cancelSubmit = true;
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文