2012-07-26 16 views
0

我想提交一個表單驗證captcha使用ajax請求後 - 如果captcha驗證成功,我必須提交表單。無法在jQuery提交表單ajax回調函數

這裏是形式:

<form action="Feedback" method="post" name="contact_us" id="contact_us_form"> 
    <table id="contact_us_table" style="font-family: 'Muli', serif;font-weight:bold;margin-left: 26px;margin-top: 39px;"> 

    <tr> 
    <td> </td> 
    <td height="20px" style="color: red;font-size: 12px;" id="msg"> </td> 
    </tr> 
    <tr> 
    <td><span style="color: red">*</span>Name </td> 
    <td> <input type="text" name="name" id="name" /> </td> 
    </tr> 

    <tr> 
    <td><span style="color: red">*</span>Email </td> 
    <td> <input type="text" name="email" id="email" /> </td> 
    </tr> 


    <tr> 
    <td width="100px;"><span style="color: red">*</span>Message </td> 
    <td> <textarea style="max-width:420px;width: 420px; height: 100;" name="message" id="message"></textarea></td> 
    </tr> 

    <tr> 
    <td height="50px;"></td> 
    <td>Human Verification.</td> 
    </tr> 




    <tr> 
    <td><img src="<%=GlobalData.SERVER_ROOT+"simpleCaptcha.png"%>"></td> 
    <td height="20px;"><input type='text' name='answer' id="answer" value=''> </td> 
    </tr> 

    <tr> 
    <td></td> 
    <td> <button class="button1" id="submit">Submit</button> </td> 
    </tr> 


    </table> 
    </form> 

這裏是JavaScript:

$("#contact_us_form").submit(function(e){ 
      e.preventDefault(); 
      var email = $("#email").val(); 
      var name = $("#name").val(); 
      var message = $("#message").val(); 
      var answer = $("#answer").val(); 



      if(name == ""){ 
        $("#name").focus(); 
        $("#msg").html("Please enter your name"); 
        return false; 
        } 
      else if(email == ""){ 
       $("#email").focus(); 
       $("#msg").html("Please enter an email"); 
       return false; 
       } 
      else if(!isValidEmailAddress(email)){ 
       $("#email").focus(); 
       $("#msg").html("Email should be like : [email protected]"); 
       return false; 
       } 
      else if(message == ""){ 
       $("#message").focus(); 
        $("#msg").html("Please write your message"); 
        return false; 
       } 
      else if(answer == ""){ 
       $("#answer").focus(); 
        $("#msg").html("Enter what you see in image."); 
        return false; 
       } 



      $.ajax({ 
       type: "POST", 
       url: "CheckCaptcha", 
       data: {answer:answer} 
      }).done(function(msg) { 
       // alert(msg); 

       if(msg == "true"){ 
        //$('#contact_us_form').submit(); 
        //$('#contact_us_form').submit(); 
         $("#contact_us_form").unbind('submit').submit(); 
        //setTimeout(document.getElementById('contact_us_form').submit(), 10); 
       } 
       else{ 
        $("#answer").focus(); 
         $("#msg").html("Captcha Incorrect"); 
         return false; 
        } 
       }).error(function(){ 

       }); 



      }); 

您可以在JavaScript看到:

if(msg == "true"){ 

        //$('#contact_us_form').submit(); 
         $("#contact_us_form").unbind('submit').submit(); 
        //setTimeout(document.getElementById('contact_us_form').submit(), 10); 
       } 

我已經嘗試了所有我可以。但它不起作用。

他們有什麼不對嗎?

編輯::

如果我點擊提交兩次此代碼的工作...

+0

「我盡我所能...」請詳細說明! – Brian 2012-07-26 16:37:47

+0

你可以在javascript中看到註釋的代碼.....我已經嘗試過所有這些代碼。 – JAVAGeek 2012-07-26 16:39:49

+0

您是否嘗試過增加TimeOut中的數字? – 2012-07-26 16:49:54

回答

1

這正因爲如此:

<button class="button1" id="submit">Submit</button> 

永遠不要使用「類型」作爲名稱或ID

我正在使用submit作爲ID。這就是爲什麼代碼不工作..

當我把它改成

<button class="button1" id="submit_button">Submit</button> 

它的工作!

0

不帶參數嘗試取消綁定:

$("#contact_us_form").unbind().submit(); 
+0

沒有工作..... – JAVAGeek 2012-07-26 16:44:48