使用 JavaScript 触发 happy.js 验证?

发布于 2025-01-05 21:24:40 字数 1084 浏览 4 评论 0原文

jQuery 验证 对于我的目的来说有点臃肿,所以我发现 Happy.js。不幸的是,只有使用“提交”按钮时才会触发验证,而我没有该按钮。 jQuery-validation 提供了一个函数“form()”来以编程方式触发验证,因此我尝试在 Happy.js 中实现该函数 – 但没有成功。我找到了一个 happy-callback 实现(commit),但这不起作用任何一个。

一些代码(更新):

$('#someFormSubmit').click(function() {
  $('#someForm').submit();
});
$('#someForm').submit(function(event) {
  event.preventDefault();
  var jqxhr = $.post('/some', $('#someForm').serialize())
    .success(function(response) {});
});
$("#someForm").isHappy({
  fields: {
    '#name': {
      required: true,
      message: "Hello"
    },
  },
  testMode: true,
  unHappy: function () { alert("hello"); },
});

回调中没有任何警报,并且“happy”始终为 true,而它应该为 false。

该表单位于 twitter-bootstrap 模式框中。由于 modal-body 和 modal-footer 是分离的,只有一个链接 .btn 可以提交,但没有正确的输入 type=submit。

有什么解决办法吗?

jQuery validation is kind of bloated for my purpose, so I found Happy.js. Unfortunately, the validation is only triggered when using a Submit-button, which I don't have. jQuery-validation offered a function "form()" to trigger the validation programmatically, so I tried to implement the function into Happy.js – without any success. I found a happy-callback implementation (commit), but this doesn't work either.

Some code (updated):

$('#someFormSubmit').click(function() {
  $('#someForm').submit();
});
$('#someForm').submit(function(event) {
  event.preventDefault();
  var jqxhr = $.post('/some', $('#someForm').serialize())
    .success(function(response) {});
});
$("#someForm").isHappy({
  fields: {
    '#name': {
      required: true,
      message: "Hello"
    },
  },
  testMode: true,
  unHappy: function () { alert("hello"); },
});

There's no alert from the callbacks and 'happy' is always true, when it should be false.

The form is in a twitter-bootstrap modal box. Due to the separation of modal-body and modal-footer there's only a link .btn to submit, but not a proper input type=submit.

Any solutions?

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

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

发布评论

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

评论(5

奢望 2025-01-12 21:24:40

尝试这样的事情:

// Remember to do all your happy validation stuff first, to ensure that jQuery
// fires off the events in the correct order.
$("#yourForm").isHappy({
  // your validation config
});

$("#yourForm").submit(function (event) {
  // This line prevents the form from actually being submitted
  event.preventDefault();

  // You can then define your actions however you like
  $.post({
    // your config here
  });
});

// When it's time to "submit" the form, it's easy
$("#yourForm").submit();

我还没有使用 happy.js 对此进行测试,但它也应该触发验证。

更新:这是我的jsfiddle。它表明它正在工作(尽管如果您检查小提琴,您会看到对此代码的一些修改)。但我不知道为什么这些字段不能在模糊时验证。这应该可以在 happy.js 中开箱即用,因此如果您遇到这个问题,您可能需要向作者发布问题。

Try something like this:

// Remember to do all your happy validation stuff first, to ensure that jQuery
// fires off the events in the correct order.
$("#yourForm").isHappy({
  // your validation config
});

$("#yourForm").submit(function (event) {
  // This line prevents the form from actually being submitted
  event.preventDefault();

  // You can then define your actions however you like
  $.post({
    // your config here
  });
});

// When it's time to "submit" the form, it's easy
$("#yourForm").submit();

I haven't tested this with happy.js, but it should trigger validation as well.

Update: Here is my jsfiddle for this. It demonstrates that it is working (although if you check the fiddle, you will see a few modifications to this code). I don't know why the fields aren't validating on blur, though. That's supposed to work out of the box with happy.js, so you might want to post an issue to the author if you are having that problem.

渔村楼浪 2025-01-12 21:24:40

您是否尝试过使用 submitButton 选项?

来自 http://happyjs.com/ 的官方文档:

submitButton (string):如果将 jQuery 选择器传递给此表单,则单击该项目时(而不是提交时)将验证表单。

Have you tried using the submitButton option?

From the official documentation at http://happyjs.com/:

submitButton (string): If you pass jQuery selector to this the form will be validated when that item is clicked instead of on submit.

榆西 2025-01-12 21:24:40

来自 HappyJS

Happy.js 现在将验证模糊事件的各个字段以及提交时的所有字段。

对模糊事件的验证还不够?

尝试将您的 HTML 代码以及您使用 JS 所做的事情写入 jsfiddle 中,将更容易获得帮助。

但是,如果您只想在单击某个按钮/元素时激活验证,只需将 jQuery 选择器设置为 happy 函数参数中的变量 submitButton

From HappyJS

Happy.js will now validate individual fields on blur events and all fields on submit.

The validation on blur events isn't enough?

Try to write your HTML code and what you're doing with JS into a jsfiddle, will be easier to get help with it.

But, if you just want to activate the validation when some button/element is clicked, just set the jQuery selector to the variable submitButton in the parameters from happy function

少年亿悲伤 2025-01-12 21:24:40

Happy.js 注册了一个提交处理程序,因此如果您需要以编程方式触发它,您应该能够使用 jQuery 触发提交,不是吗?

$(form).submit()

Happy.js registers a submit handler, so if you need to trigger it programmatically you should be able to just trigger submit with jQuery, no?

$(form).submit()
巨坚强 2025-01-12 21:24:40
$('#my_form').isHappy({
    when: "blur keyup"
});

此语法适用于当前版本的 Happy.js。

$('#my_form').isHappy({
    when: "blur keyup"
});

This syntax works for the current version of Happy.js.

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