[英]ValidationEngine doesn't work for a form dynamically created
快速創建表單后,validationEngine不再起作用
$(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);
});
我已經配置了ValidationEngine,如下所示:
// 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]');
這里驗證的列表中,您可以方括號在上述行之間使用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.