jQuery validate().element() 不直接显示错误

发布于 2024-12-27 14:50:58 字数 515 浏览 3 评论 0原文

我正在使用 jQuery UI 选项卡控件来实现一个简单的向导。为了进行验证,我使用 jQuery 验证。当按“下一步”按钮移至向导的下一个选项卡时,我会验证当前选项卡上的所有内容:

$('.forward').click(function () {
  var valid = true;
  $(this).closest("div").find("input").each(function () {
    valid = valid && $("#form").validate().element(this);
  });

  // Code to select next tab if valid
});

这有效 - 我在 valid 中获得了正确的值。但是,如果出现任何问题,错误消息不会立即显示。我必须单击其他地方才能使下一个按钮在错误消息出现之前失去焦点。

如何让错误消息在运行 validate().element(this) 后立即出现?

I'm using a jQuery UI tab control to implement a simple wizard. For validation I'm using jQuery validation. When pressing the "next" button to move on to the next tab of the wizard, I validate everything on the current tab:

$('.forward').click(function () {
  var valid = true;
  $(this).closest("div").find("input").each(function () {
    valid = valid && $("#form").validate().element(this);
  });

  // Code to select next tab if valid
});

This works - I get the correct value in valid. However, if anything is wrong, the error message doesn't show up immediately. I have to click somewhere else to make the next button lose focus before the error message appears.

How can I make the error messages appear immediately after running validate().element(this)?

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

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

发布评论

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

评论(1

ゃ懵逼小萝莉 2025-01-03 14:50:58

您的方法的行为类似于模糊或 keyup 的验证,但返回结果。
这里没有模糊或按键。
尝试

$('.forward').click(function () {
  var valid = true;
  $(this).closest("div").find("input").each(function () {
    valid = valid && $("#form").validate().element(this);
    this.blur();
  });
// Code to select next tab if valid
});

Your method behaves as validation on blur or keyup, but returns the result.
You don't have blur or keyup here.
try

$('.forward').click(function () {
  var valid = true;
  $(this).closest("div").find("input").each(function () {
    valid = valid && $("#form").validate().element(this);
    this.blur();
  });
// Code to select next tab if valid
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文