使用 JavaScript 触发 happy.js 验证?
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试这样的事情:
我还没有使用 happy.js 对此进行测试,但它也应该触发验证。
更新:这是我的jsfiddle。它表明它正在工作(尽管如果您检查小提琴,您会看到对此代码的一些修改)。但我不知道为什么这些字段不能在模糊时验证。这应该可以在 happy.js 中开箱即用,因此如果您遇到这个问题,您可能需要向作者发布问题。
Try something like this:
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.
您是否尝试过使用
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.来自 HappyJS
对模糊事件的验证还不够?
尝试将您的 HTML 代码以及您使用 JS 所做的事情写入 jsfiddle 中,将更容易获得帮助。
但是,如果您只想在单击某个按钮/元素时激活验证,只需将 jQuery 选择器设置为 happy 函数参数中的变量
submitButton
From HappyJS
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 functionHappy.js 注册了一个提交处理程序,因此如果您需要以编程方式触发它,您应该能够使用 jQuery 触发提交,不是吗?
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?
此语法适用于当前版本的 Happy.js。
This syntax works for the current version of Happy.js.