在飞行中创建表单后验证engine不再起作用
$(window).load(function($) {
var form = document.createElement('form');
form.setAttribute('id', 'xform');
var lbl_usr = document.createElement('label');
var input_usr = document.createElement('input');
input_usr.setAttribute('id', 'user');
input_usr.setAttribute('name', 'user');
input_usr.setAttribute('class', 'user-txt');
var submit = document.createElement('input');
submit.setAttribute('type', 'submit');
submit.setAttribute("value", "login");
submit.setAttribute('id', 'login');
form.appendChild(lbl_usr);
form.appendChild(input_usr);
form.appendChild(submit);
var divLog = document.getElementById('log');
divLog.appendChild(form);
});
我已经配置了验证engine,如显示的bellow:
// I have tried attach to the form the validationEngine
// on document ready, load and without both but it doesn't working
$("#xform").validationEngine('attach',{
onValidationComplete: function(form, status){
$("#xform").bind('submit', function(e) {
e.preventDefault();
});
},
maxErrorsPerField: 1,
custom_error_messages : {
// Custom Error Messages for Validation Types
'.user-txt': {
'minSize': {
'message': "less than required."
},
'required': {
'message': "required"
}
}
}
});
$(document).on('click', '#login', function (e) {
$('#xform').validationEngine('validate');
});
// I have tried this but without success for a dynamically form
// created, for a static form this works fine
$('#login').click(function() {
$('#xform').validationEngine('validate');
});
});
我不知道为什么这不起作用,此代码出了什么问题...
一些帮助会很棒
谢谢
问题在于您的代码以外的任何其他内容。在什么情况下,您的表格得到了验证?只需添加此行(Posabsolute的jQuery验证引擎能够从HTML5数据属性读取选项):
input_usr.setAttribute('data-validation-engine', 'validate[required]');
在此处,您可以在上线中的方括号之间使用验证器的列表。