2014-10-10 110 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