2015-04-26 67 views
0

使用jQuery驗證插件做驗證的引導模式窗體,當我發送的形式,jQuery的驗證插件是工作,但Ajax代碼會做兩個時間和形式不能發送的形式出。jQuery驗證插件AJAX提交不工作

引導形式

<form class="contact"> 
    <fieldset> 
     <div class="modal-body"> 
      <ul class="nav nav-list"> 
       <div class="form-group"> 
        <label for="topic" class="control-label ">topic</label> 
        <input class="form-control" type="text" name="topic" /> 
        <span class="help-block"></span> 
       </div> 
       <div class="form-group"> 
        <label for="ruser" class="control-label "> ruser: </label> 
        <input class="form-control" type="text" name="ruser" value="<?php echo $username; ?>" readonly/> 
        <span class="help-block"></span> 
       </div> 
       <div class="form-group"> 
        <label for="content" class="control-label ">content:</label> 
        <textarea class="form-control" name="content" rows="3"></textarea> 
        <span class="help-block"></span> 
       </div> 
      </ul> 
     </div> 
    </fieldset> 
    <button class="btn btn-success" id="submitcontact">ok</button> 
</form> 

的JavaScript

$(document).ready(function() { 
    $('form').validate({ 
     rules: { 
      topic: { 
       required: true 
      }, 
      ruser: { 
       required: true 
      }, 
      content: { 
       required: true 
      } 
     }, 

     messages: { 
      topic: { 
       required: 'enter topic' 
      }, 
      ruser: { 
       required: 'enter nuser' 
      }, 
      content: { 
       required: 'enter content' 
      } 
     }, 
     highlight: function (element) { 
      $(element).closest('.form-group').addClass('has-error'); 
     }, 
     unhighlight: function (element) { 
      $(element).closest('.form-group').removeClass('has-error'); 
     }, 
     errorElement: 'span', 
     errorClass: 'help-block', 
     errorPlacement: function (error, element) { 
      if (element.parent('.input-group').length) { 
       error.insertAfter(element.parent()); 
      } else { 
       error.insertAfter(element); 
      } 
     }, 

     submitHandler: function (form) { 
      $.ajax({ 
       type: "POST", 
       url: "process.php", 
       data: $('form.contact').serialize(), 
       success: function (msg) { 
        alert("ok!"); 
        if (msg == 'ok') { 
         alert(msg); 
         location.reload() 
        } 
       }, 
       error: function() { 
        alert("failure"); 
       } 
      }); 
      return false; 
     } 
    }); 

    $('#submitcontact').click(function() { 
     $('form.contact').submit(); 
    }) 
}); 

我怎樣才能解決這個問題?

jsfiddle

+0

能否請您標記的答案,如果它幫助? –

回答