0

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 : john@example.com");
               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);
                 }

できることはすべて試しました。しかし、うまくいきませんでした。

彼らは何か間違っていますか?

編集 ::

このコードは、送信を 2 回クリックすると機能します。

4

2 に答える 2

1

それはちょうどこのためです:

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

「type」を名前または ID として使用しないでください

submitIDとして使用していました。そのため、コードが機能していませんでした..

に変更したとき

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

出来た !

于 2012-08-13T12:01:19.040 に答える