Pat*_*ick 2 javascript validation jquery callback
为了我的目的,jQuery验证有点臃肿,所以我找到了Happy.js.不幸的是,只有在使用我没有的提交按钮时才会触发验证.jQuery-validation提供了一个函数"form()"来以编程方式触发验证,因此我尝试将该函数实现到Happy.js中 - 没有任何成功.我找到了一个快乐的回调实现(提交),但这也不起作用.
一些代码(更新):
$('#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"); },
});
Run Code Online (Sandbox Code Playgroud)
回调没有警报,而'happy'总是如此,当它应该是假的.
表单位于twitter-bootstrap模式框中.由于模态体和模态页脚的分离,只有一个链接.btn要提交,但不是正确的输入类型=提交.
有解决方案吗
尝试这样的事情:
// 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();
Run Code Online (Sandbox Code Playgroud)
我没有用happy.js测试过这个,但它也应该触发验证.
更新:这是我的jsfiddle.它表明它正在工作(虽然如果你检查小提琴,你会看到对这段代码的一些修改).不过,我不知道为什么这些字段没有验证模糊.这应该与happy.js一起开箱即用,所以如果遇到这个问题,你可能想向作者发帖.
| 归档时间: |
|
| 查看次数: |
3093 次 |
| 最近记录: |