2014-10-10 62 views
-1

我有一個小的註冊表單,我嘗試使用引導程序的submitHandler提交此表單,但它給了我一個Uncaught TypeError: Cannot read property 'attr' of null提交我的引導表單時出現jQuery錯誤

我使用此代碼來驗證並提交:

$(document).ready(function(){ 
     //To validate the registration form and save its value after validation 
     $('#registerForm').bootstrapValidator({ 
      message: 'This value is not valid', 
      feedbackIcons: { 
       valid: 'glyphicon glyphicon-ok', 
       invalid: 'glyphicon glyphicon-remove', 
       validating: 'glyphicon glyphicon-refresh' 
      }, 
      fields: { 
        email: { 
        validators: { 
         notEmpty: { 
           message: 'The email is required and cannot be empty' 
         }, 
         emailAddress: { 
          message: 'The input is not a valid email address' 
         } 
        } 
       },     
       password: { 
        validators: { 
         notEmpty: { 
          message: 'The password is required and cannot be empty' 
         } 
        } 
       }, 
       confirmPassword: { 
          validators: { 
         notEmpty: { 
          message: 'The password is required and cannot be empty' 
         } 
        } 
       }, 
       submitHandler: function(form) { // <- only fires when form is valid 
        $.ajax({ 
         type: 'POST', 
         url: 'include-ajax/check_and_save_registration_form.php', 
         data: $(form).serialize(), 
         success: function() { 
          $(form).fadeOut(500, function(){ 
           $(form).html("USER DONE!").fadeIn(); 
          }); 
         } 
        });   // <- end '.ajax()' 
        return false; // <- block default form action 
       } 
      } 
     }); 
    }); 

我爲什麼會得到這個錯誤?當我刪除submitHandler時,我沒有得到那個錯誤,但我需要它將表單數據存儲在我的數據庫中。

數據驗證正確但未提交。我也使用submitHandler: function(validator, form, submitButton)。 仍然給出相同的錯誤。 如何提交表單而不會出錯?

我的HTML:

<div class="pages_container"> 
    <form id="registerForm" method="post" class="form-horizontal"> 
     <div class="form-group"> 
      <label class="col-lg-3 control-label">Email address</label> 
      <div class="col-lg-6"> 
       <input class="form-control" name="email" type="email" /> 
      </div> 
     </div>    
     <div class="form-group"> 
      <label class="col-lg-3 control-label">Password</label> 
      <div class="col-lg-6"> 
       <input type="password" class="form-control" name="password" /> 
      </div> 
     </div> 

     <div class="form-group"> 
      <label class="col-lg-3 control-label">Retype password</label> 
      <div class="col-lg-6"> 
       <input type="password" class="form-control" name="confirmPassword" /> 
      </div> 
     </div> 

     <div class="form-group"> 
      <div class="col-lg-6 col-lg-offset-3"> 
       <button type="submit" class="btn btn-primary btn-lg btn-block">Register</button> 
      </div> 
     </div>  
     </div> 
    </form> 
</div> 
+0

爲什麼你有兩個獨立的'email'規則的任何特別的原因? – PeterKA 2014-10-10 12:43:36

+0

@ user3558931我更新了代碼 – 2014-10-10 12:53:41

+0

你確定bootstrap-validator有一個'submitHandler'選項嗎? – PeterKA 2014-10-10 13:04:36

回答

1

請參閱此頁的正確方法通過AJAX提交表單:Using Ajax to Submit the Form

$(document).ready(function() { 
    $(form) 
     .bootstrapValidator({ 
      ... options ... 
     }) 
     .on('success.form.bv', function(e) { 
      // Prevent form submission 
      e.preventDefault(); 

      // Get the form instance 
      var $form = $(e.target); 

      // Get the BootstrapValidator instance 
      var bv = $form.data('bootstrapValidator'); 

      // Use Ajax to submit form data 
      $.post($form.attr('action'), $form.serialize(), function(result) { 
       // ... Process the result ... 
      }, 'json'); 
     }); 
}); 
+0

非常感謝您的回答,它適用於我我現在可以保存在數據庫中,併爲我缺乏有關bootstrap的信息感到遺憾,這是我第一次。我只是想問,是否可以在用「謝謝你的文本」提交之後將用戶重定向到註冊頁面,因爲在提交後將它重定向到「頁面」操作? – 2014-10-10 13:12:07

+0

很高興它的工作。不要忘記接受答案。聽起來你需要編輯你的服務器端腳本來做重定向。 – PeterKA 2014-10-10 13:17:34