jQuery 在运行时删除validationEngine

发布于 2025-01-07 22:44:17 字数 1121 浏览 1 评论 0原文

我正在使用validationEngine脚本来验证表单。我初始化它:

$("#formid").validationEngine();

我并没有真正通过提交按钮提交表单。我使用带有 onclick 事件的锚元素,该事件调用 saveElement() 函数。在这个函数中我调用:

$("#formid").validationEngine('validate');

效果很好。

之后,我重置了字段(例如,将输入字段的内容设置为 ""),通过该字段,所需的字段开始显示错误。

为了防止发生这些错误,我想从表单元素中删除validationEngine。但我就是无法删除它。我尝试了各种选项,包括下面的选项,但都不起作用。

$('#formid').validationEngine('hideAll'); // To hide the error messages
$('#formid').validationEngine('detach');  // To remove the validationEngine

有人建议我如何删除验证引擎吗?

解决方法

看起来确实没有解除绑定的方法。我已经在 github 项目中添加了一个问题。也许他们会实现一个解除绑定的方法: https://github.com/posabsolute/jQuery-Validation-Engine/issues/287

作为解决方法,我想出了以下解决方案: 您可以:

  • 使用 jQuery.clone() 复制没有事件的表单,删除原始表单并再次将validationEngine添加到表单中
  • 删除表单元素上的验证类,将它们存储在数组中并在重置后再次插入它们表单

仍然打开

也许有更好的解决方案或解决方法。如果您知道,请告诉我。

I'm using the validationEngine script to validate a form. I init it with:

$("#formid").validationEngine();

I do not really submit the form through a submit button. I use an anchor element with an onclick event which calls a saveElement() function. In this function I call:

$("#formid").validationEngine('validate');

which works just fine.

Right after that, I reset the fields (for example, the content of input fields to "") through which the fields which are required start showing errors.

To prevent those errors from occurring, I would like to remove the validationEngine from the form element. But I just can't remove it. I tried various options, including the ones below, but none of them worked.

$('#formid').validationEngine('hideAll'); // To hide the error messages
$('#formid').validationEngine('detach');  // To remove the validationEngine

Does anyone have a suggestion how I can remove the validationEngine?

Workaround

It really seams like there is no unbind method. I've added a issue into the github project. Maybe they will implement an unbind method:
https://github.com/posabsolute/jQuery-Validation-Engine/issues/287

As a workaround I came up with the following solutions:
You could either:

  • Use jQuery.clone() to copy the form without the events, remove the original form and add the validationEngine to the form again
  • Remove the validation classes on the form elements, store them in an array and insert them again after resetting the form

Still open

Maybe there is a better solution or workaround. If you know one, please let me know.

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

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

发布评论

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

评论(2

懒的傷心 2025-01-14 22:44:17

这个问题仍然没有解决。我需要在现有应用程序中禁用验证,以允许在不验证表单的情况下发生回发事件。我通过使用如下正则表达式删除验证类成功地做到了这一点:

// Work around
$('[class*="validate"]').each(function () {
      var _this = $(this);
      _this.attr("class", _this.attr("class").replace(/validate\[.*]+\s?/, ""));
});

This problem is still not fixed. I needed to disable validation in an existing app to allow postback events to take place without validating the form. I successfully did so by removing the validation classes with a regular expression like this:

// Work around
$('[class*="validate"]').each(function () {
      var _this = $(this);
      _this.attr("class", _this.attr("class").replace(/validate\[.*]+\s?/, ""));
});
离不开的别离 2025-01-14 22:44:17

我认为你真的是过度设计了。这可能不是您期望的答案,但我认为您应该这样做:

首先,在要提交的锚元素上,执行以下操作:

$('#anchorElement').click(function() {
    $('#formid').submit()
})

然后,您可以这样做以在提交后删除表单:

$('#formid').submit(function() {
    // Do your submitting stuff, like validating.
    $(this).remove()
    // Or use $(this).hide() if you simply want to hide it.
})

但我不会不提倡使用锚元素的方法。几乎每个网站都必须进行良好的降级。

问题是,当您的用户没有启用 JavaScript 时,您的表单将永远不会被提交。

顺便说一句,看起来验证引擎没有“解除绑定”。请参阅此处的解决方案:从表单中删除 JQueryvalidationEngine

I think you're really overengineering things. This may not be the answer you expected, but here is what I think you should do:

Firstly, on the anchor element to submit, do this :

$('#anchorElement').click(function() {
    $('#formid').submit()
})

Then, you can do it like this to remove the form after submitting it:

$('#formid').submit(function() {
    // Do your submitting stuff, like validating.
    $(this).remove()
    // Or use $(this).hide() if you simply want to hide it.
})

But I wouldn't advocate the method of using an anchor element. Nicely degrading is a must for almost every website.

The problem is, when your user won't have javascript enabled, your form will never be submitted.

Btw, it looks like there is no "unbinding" of validationengine. See here for a solution: Remove JQuery validationEngine from form

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文