0
創建表單後的飛行形式工作validationEngine不工作了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');
});
});
我不知道爲什麼這不起作用,這個代碼有什麼問題...
有些幫助會很大
謝謝
在執行所謂的附加驗證引擎時,$('#xform')是空對象。因爲它還沒有創建。控制檯是否顯示說明這一事實的任何錯誤? – px5x2
不,控制檯不顯示任何有關它的錯誤。但是用$(document).on('click','#login'預計#login submit btn將在稍後接收事件並且當表單在DOM中可用時,對吧? – Bera