2013-10-17 59 views
1

我想在發送ajax請求之前使用jQuery驗證引擎驗證我的表單。但是如果表單中包含錯誤,Ajax就會發送請求。jQuery驗證引擎屬性不起作用

的JavaScript:

$('#addphrase').validationEngine('attach', { 
    inlineValidation: false, 
    promptPosition: "centerRight", 
    onSuccess: function() { 
     use_ajax = true; 
     alert(dcd); 
    }, 
    onFailure: function() { 
     use_ajax = false; 
     alert(ggg); 
    } 
}); 

$('form[name="addphrase"]').submit(function (e) { 
    if (use_ajax) { 
     $('#loading1').css('visibility', 'visible'); 
     $.get('addphrase.php', $(this).serialize() + '&ajax=1', 

     function (data) { 
      if (parseInt(data) == -1) { 
       $("#resp1").show('slow').after('<p id="resp-mes1" style=" color:#000000; text-decoration: bold;">* Please ensure all fields are filled.","error"</p>'); 
      } else { 
       $("#resp1").show('slow').after('<p id="resp-mes1" style=" color:#000000; text-decoration: bold;">Added successfully. Thank You...</p>'); 
      } 
      $('#loading1').css('visibility', 'hidden'); 
      setTimeout("jQuery('#resp1').hide('slow');", 3000); 
      setTimeout("jQuery('#resp-mes1').hide('slow');", 5000); 
     }); 
    } 
    e.preventDefault(); 
} 
}); 

編輯 HTML

<form class="form-horizontal" id="addphrase" name="addphrase" method="GET" action=""> 
      <div class="control-group"><label class="control-label" for="form-field-1">Phrase</label> 
    <div class="controls"> 
    <input type="text" id="phrase" class="validate[required]" placeholder="Phrase" name="Phrase" value="" /></div></div> 
    <div class="control-group"><label class="control-label" for="form-field-11">Content Here</label> 
    <div class="controls"> 
    <textarea name="post_content" value="" class="validate[required] autosize-transition span12" id="comment" style="overflow: hidden; word-wrap: break-word; resize: horizontal; height: 67px;"></textarea></div></div> 
    <div class="space-4"></div><div class="control-group" style="float:left; margin-right:25px"><div class="controls"> 
    <button type="submit" class="btn btn-info"> 
<i class="icon-ok bigger-110"></i> 
<input type="submit" value="" id="phsubmit" style="opacity:0">Submit</button> 
<button type="reset" class="btn"><i class="icon-undo bigger-110"></i>Reset</button></div></div> 
<div id="resp1" style="float:left; margin-top:5px"><img id="loading1" style="visibility:hidden;" src="assets/img/ajax-load.gif" width="16" height="16" alt="loading" /></div></div> 
</form> 

在上面的代碼將不會自動顯示警告,如果它失敗,它將不會附加屬性,在這兩種情況下,或成功。

任何幫助,將不勝感激。

+0

顯示你的HTML代碼 –

+0

是你''form'和name''id'是一樣的嗎?將您的'html'代碼 –

+0

@Tushar Gupta,@ Ajith S添加Html表格 – james

回答

1

嘗試使用onValidationComplete一樣,

$('#addphrase').validationEngine('attach', { 
    inlineValidation: false, 
    promptPosition: "centerRight", 
    onValidationComplete: function (form, status) { 
     if (status) { 
      alert('Form submit'); 
      $('#loading1').css('visibility', 'visible'); 
      $.get('addphrase.php', $(this).serialize() + '&ajax=1', 

      function (data) { 
       if (parseInt(data) == -1) { 
        $("#resp1").show('slow').after('<p id="resp-mes1" style=" color:#000000; text-decoration: bold;">* Please ensure all fields are filled.","error"</p>'); 
       } else { 
        $("#resp1").show('slow').after('<p id="resp-mes1" style=" color:#000000; text-decoration: bold;">Added successfully. Thank You...</p>'); 
       } 
       $('#loading1').css('visibility', 'hidden'); 
       setTimeout("jQuery('#resp1').hide('slow');", 3000); 
       setTimeout("jQuery('#resp-mes1').hide('slow');", 5000); 
      }); 
     } 
     else 
     { 
      alert('Error'); 
     } 
    }, 
}); 

Docs

Demo

+0

不與您的代碼一起工作 – james

+0

這裏發生的是'promptPosition:'centerRight', onSuccess:function(){ use_ajax = true; alert(dcd); ()# $('#addphrase')。submit(); //在此處提交表單 }, onFailure:function(){ use_ajax = false; alert(ggg); }這些行根本沒有執行。請告訴我在這些行中有任何錯誤 – james

+0

@james我改變了我的代碼,嘗試一次。 –