2014-02-05 79 views
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'); 
    }); 

    }); 

我不知道爲什麼這不起作用,這個代碼有什麼問題...

有些幫助會很大

謝謝

+0

在執行所謂的附加驗證引擎時,$('#xform')是空對象。因爲它還沒有創建。控制檯是否顯示說明這一事實的任何錯誤? – px5x2

+0

不,控制檯不顯示任何有關它的錯誤。但是用$(document).on('click','#login'預計#login submit btn將在稍後接收事件並且當表單在DOM中可用時,對吧? – Bera

回答

1

問題是你的代碼只有驗證器。您的表單在什麼條件下得到驗證?剛加入這一行(posabsolute的jQuery驗證引擎能夠讀取來自HTML5數據屬性選項):你可以在上面的行方括號內使用驗證的

input_usr.setAttribute('data-validation-engine', 'validate[required]'); 

Here名單。

相關問題