【问题标题】:Unable to submit form in jquery ajax callback function无法在 jquery ajax 回调函数中提交表单
【发布时间】:2012-07-25 07:23:53
【问题描述】:

我正在尝试使用 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);
                 }

我已经尽力了。但它没有用。

他们有什么问题吗?

编辑 ::

如果我点击两次提交,此代码将起作用..

【问题讨论】:

  • “我已经尽力了……”请详细说明!
  • 你可以在javascript中看到注释代码.....我都试过了......
  • 您是否尝试过增加 TimeOut 内的数字?
  • 是的,我尝试了 1000 毫秒,但没有成功。
  • 就个人而言,我会拥有它,以便 PHP 负责您的所有验证和验证码,然后让 jQuery 返回它所具有的任何错误。如果您有多个错误,请使用数组显示它们。如果一切正常,通过 PHP 发送表单并返回成功消息。

标签: javascript ajax forms submit


【解决方案1】:

正因为如此:

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

切勿使用“类型”作为名称或 ID

我使用 submit 作为 id。这就是代码不起作用的原因..

当我把它改成

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

成功了!

【讨论】:

    【解决方案2】:

    尝试不带参数解绑:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-22
      • 1970-01-01
      相关资源
      最近更新 更多