【发布时间】:2012-02-17 09:54:50
【问题描述】:
我正在尝试设置 validval 插件(最新 4.0.0)版本,但没有任何运气...
这是我在 HTML
中的内容<form name="LoginForm" id="LoginForm" data-transition="slide" action="somepage.html" method="post">
// ... text inputs
<input type="submit" id="CheckLogin" name="CheckLogin" value="Login" data-theme="e" />
</form>
还有 Jquery:
// tried pageinit and pageshow
$('div:jqmData(role="page")').live('pageshow', function() {
$(this).find('form').each(function() {
// call once
if ( $(this).find('.required').length > 0 && $(this).attr('valed') != 'on') {
// flag and validVal
$(this).attr('valed', 'on').validVal({
'validate': { 'onBlur': false },
'customValidations': { },
'form': {
'onValid': function( $form, language ) {
console.log("form ok");
},
'onInvalid': function( $form, language ) {
console.log("form not ok");
// attach an error to form
$(this).attr('error', 'yes');
}
}
});
}
});
这应该在 pageshow 上设置表单验证。
现在我绑定到提交按钮:
$("#CheckLogin").click( function(event) {
event.preventDefault();
// call validval
// DOES NOT WORK, cause validval() doesn't fire like this
var errors = $("#CheckLogin").triggerHandler( "submitForm.vv" );
console.log( errors );
// alternative, check for attached errors
// DOES NOT WORK, cause validval() doesn't fire on click
// ... my routine running if there are no errors
// Submit
// document.LoginForm.submit()
});
问题是,如果我绑定提交,验证工作,但表单总是立即提交,不管 validval 说什么。所以我现在绑定点击并想“手动”触发我的例程。这意味着我还需要使用 triggerhandler 触发 validval - 至少在他们的 website 上说要这样做。问题是,validval 不会像这样触发。我也不知道为什么?
问题:
如何从我的点击处理程序中触发 validVal() 表单验证?或者,如果我改回绑定提交,如何确保表单不会自动提交到遗忘状态?
希望信息足够,非常感谢您的帮助!
【问题讨论】:
标签: jquery html plugins jquery-mobile validation