2014-02-07 43 views
0

我正在使用jQuery進行驗證碼。我想阻止form.php的提交時data='false'如何防止我的表單提交後的結果

這裏是我迄今爲止

$(document).ready(function(){ 
    $("#submit").click(function() { 
     val=$('#vercode').val(); 
     $.ajax({ 
      type:"post", 
      url:"verify-captcha.php", 
      data:{'code':val}, 
      success:function(data){ 
       if(data=='false') { 
        $("#msg").html(data); 
       } 
      } 
     }); 
    }); 
}); 
+0

謝謝你的回覆,我得到了解決方案。 – Vinie

回答

1

使用返回false聲明:

if(data=='false'){ 
    return false; 
} 
0

爲了防止表單提交使用event.preventDefault();

$(document).ready(function(){ 
    $("#form").submit(function(event) { 
     val=$('#vercode').val(); 
     $.ajax({ 
      type:"post", 
      url:"verify-captcha.php", 
      data:{'code':val}, 
      success:function(data){ 
       if(data=='false') { 
        event.preventDefault(); 
        $("#msg").html(data); 
       } 
      } 
     }); 
    }); 
}); 
+0

你的代碼仍然提交表單。 –

+0

是的,這是預期的行爲,OP要求防止表單提交僅在驗證碼驗證失敗。 – Eternal1

+0

@TwiStar我會用提交事件監聽器替換click事件監聽器 - 也可以通過鍵盤來提交表單。 –