为什么需要2次点击才能提交​​表格

时间:2014-08-18 05:20:27

标签: php jquery ajax

我通过两个提交按钮提交表单,但为什么需要2次点击才能提交​​表单 我的表格如下所示:

<form id="mychkform" method="POST" class="">
   <input type="email" class="form-control" id="email" name="email" 
   placeholder="Enter email" data-original-title="" title="" />
   <input type="radio" name="cnt_as" checked="checked" value="cnt_as_guest"/>
   I want to continue as guest<br/>
    <input type="radio" name="cnt_as" style="margin-top:10px" value="auth_fm_chk"/> 
    I have Password
    <div class="input-group gst_chk">  
     <input type="submit" class="btn btn-red" id="cntgyes" value="Continue as Guest">
    </div>
    <div class="sggn_in" style="display:none">
       <div class="input-group">
          <span class="input-group-addon"><i class="fa fa-lock"></i></span>
           <input type="password" class="form-control" id="password" name="password" placeholder="Password" data-original-title="" title="" />
             </div><br/>
                <div class="input-group">  
                  <input type="submit" class="btn btn-red" id="cntaurth" value="Sign In">
                  <span id="loading" style="display:none"><img src="<?php echo $dir; ?>img/2.gif" /> Please wait</span>
                  </div>
              </div>
</form>

我的JQuery如下所示:

$(document).ready(function(){ 
 $('input[type="radio"]').click(function(){
    if($(this).attr("value")=="auth_fm_chk"){
        $(".sggn_in").show();
        $(".gst_chk").hide();
    }
     if($(this).attr("value")=="cnt_as_guest"){
        $(".sggn_in").hide();
        $(".gst_chk").show();
    }
});
$("form#mychkform").submit(function(event) {
event.preventDefault();
validate();
  });
 function validate(){
   $('#cntgyes').click(function(){   
    function checkEmail(){
    var email = $("#email").val();
    if((email.length < 6) ||
     (email.indexOf('@',0) < 1) ||
     (email.lastIndexOf('@') != email.indexOf('@',0)) ||
     (email.lastIndexOf('@') > (email.length - 5)) ||
     (email.lastIndexOf('.') > (email.length - 3)) ||
     (email.lastIndexOf('.') < (email.length - 4)) ||
     (email.indexOf('..',0) > -1) ||
     (email.indexOf('@.',0) > -1) ||
     (email.indexOf('.@',0) > -1) ||
     (email.indexOf(',',0) > -1)){
         $("#email").css("border-color","#F05F68");
        return false;
    }
    else{
        $("#email").css("border-color","#0C9");
        return true;
    }   
}

if(!checkEmail()){
    return false;   
    }
    else{
        var email = $("#email").val();
        var dataString = 'email=' + email;
        $("#loading").show();
        $.ajax({
          url: "includes_fm/form_submi/checkou_guauth",
          type: "POST",
          data: dataString,
          success: function (msg) {
             $("#loading").hide();
               location.replace("checkout/guest_ckot?id_gst="+msg+"#pg2");
              }
       });
    }  
    $('#cntaurth').click(function(){      
      function checkEmail(){
        var email = $("#email").val();
        if((email.length < 6) ||
         (email.indexOf('@',0) < 1) ||
         (email.lastIndexOf('@') != email.indexOf('@',0)) ||
         (email.lastIndexOf('@') > (email.length - 5)) ||
         (email.lastIndexOf('.') > (email.length - 3)) ||
         (email.lastIndexOf('.') < (email.length - 4)) ||
         (email.indexOf('..',0) > -1) ||
         (email.indexOf('@.',0) > -1) ||
         (email.indexOf('.@',0) > -1) ||
         (email.indexOf(',',0) > -1)){
             $("#email").css("border-color","#F05F68");
            return false;
        }
        else{
            $("#email").css("border-color","#0C9");
            return true;
        }   
    }
            function checkPass(){
            var password = $("#password").val();
            if(password == 0){
                $("#password").css("border-color","#F05F68");
                return false;   
            }
            else{
                $("#password").css("border-color","#0C9");
                return true;    
            }
        }
    if(!checkEmail() || !checkPass()) {
        return false;   
    }
    else{
        var email = $("#email").val();var password = $("#password").val();
        var dataString = 'email=' + email + '&password=' + password ;
        $("#loading").show();
            $.ajax({
               url: "includes_fm/form_submi/login_submit",
               type: "POST",
               data: dataString,
               success: function (msg) {
                   if (msg == 1) {
                     $("#loading").hide();
                     window.location.reload();
                    } else {
                    $("#loading").hide();
                    $("#messagesuccerr").html("<span style=color:red;
                      font-size:14px>Oops!! Wrong Credentials!!</span>")
                     .fadeIn().delay(4000).fadeOut();
                   return false;
                  }
                }
         });
    }
});           
}); 

当我点击Continue As Guest的提交按钮时,我必须按两次按钮,然后表单验证该字段。但是为什么需要2次点击。

验证电子邮件字段后发生了同样的情况。请协助。

2 个答案:

答案 0 :(得分:1)

你能试试吗

    $("form#mychkform").submit(function(event) {    
    var flag = validate();
    if(!flag){
    event.preventDefault();
    }
   });

function validate(){

   var email = $("#email").val();
if((email.length < 6) ||
 (email.indexOf('@',0) < 1) ||
 (email.lastIndexOf('@') != email.indexOf('@',0)) ||
 (email.lastIndexOf('@') > (email.length - 5)) ||
 (email.lastIndexOf('.') > (email.length - 3)) ||
 (email.lastIndexOf('.') < (email.length - 4)) ||
 (email.indexOf('..',0) > -1) ||
 (email.indexOf('@.',0) > -1) ||
 (email.indexOf('.@',0) > -1) ||
 (email.indexOf(',',0) > -1)){
     $("#email").css("border-color","#F05F68");
    return false;
}
else{
    $("#email").css("border-color","#0C9");
    return true;
}   

}

答案 1 :(得分:1)

  • 您的validate函数有很多错误(例如不必要的$('#cntaurth').click(function(){});)。

  • span id="loading"位置不正确。

  • div id="messagesuccerr"缺失。

  • 以及其他一些问题。

Fiddle with full code

validate函数应如下所示:

function validate()
{
    if (!checkEmail())
    {
        return false;   
    }
    var isUser = $(".sggn_in").is(':visible');
    if (isUser)
    {
        if (!checkPassword())
        {
            return false;
        }
        var url = "includes_fm/form_submi/login_submit";
        var dataString = 'email=' + $("#email").val() + '&password=' + $("#password").val();
        makeRequest(url, dataString, successUserRequest);
    }
    else
    {
        var url = "includes_fm/form_submi/checkou_guauth";
        var dataString = 'email=' + $("#email").val();
        makeRequest(url, dataString, successGuestRequest);
    }
}