返回true不能在jQuery中工作

时间:2017-10-22 12:33:01

标签: javascript php jquery

我对jQuery有点新鲜。我有jQuery代码如下:

function callfunction() {
  var email = $("#email").val();
  var passwrd = $("#password").val();
  $.ajax({
    url: "checklogin.php?email="+email+"&pass="+passwrd,
    type: "post",
    success: function (response) {
      if(response == '1') {
        alert('Login Successfull. Proceed to pay now');
        $('#getre').show();
        $('#signuplink').hide();
        $('#password2').hide();
        $('#login').hide();
        $('#email').attr('disabled','disabled');
        return true;
      }
      if(response == '0') {
        alert('Login Failed !!! Try Again');
        return false;
      }
    },
    error: function(jqXHR, textStatus, errorThrown) {
      console.log(textStatus, errorThrown);
    }
  });
}

及其HTML代码如下:

<form class='form form-inline' style="margin-top:20px;" method="post" 
action="#">
  <input type="text" name="email" id="email" class="amount form-control required" Placeholder="Enter Email ID"  required="">
  <input type="password" name="password" id="password" class="amount form-control required" Placeholder="Enter Password" required="">
  <input type="submit" name="getr" class="mob-btn btn btn-primary btn-info-full" value="Login" onClick="return callfunction()">
</form>

当我提交表单并response ==0时,表单正在提交数据,而我不想提交数据是response=0

我怎么能做到这一点?

1 个答案:

答案 0 :(得分:2)

从提交按钮元素中取出type="submit"。这将阻止在ajax请求被触发之前提交表单。

<input name="getr" class="mob-btn btn btn-primary 
        btn-info-full" value="Login" onClick="callfunction()"> 

然后当您收到ajax回复时,您可以像这样致电form.submit()

$.ajax({
    url: "checklogin.php?email="+email+"&pass="+passwrd,
    type: "post",
    success: function (response) {
     if(response == '1')
     {

         $('.form-inline').submit();
         alert('Login Successfull. Proceed to pay now');

         $('#getre').show();
         $('#signuplink').hide();
         $('#password2').hide();
         $('#login').hide();
         $('#email').attr('disabled','disabled');
     }
     if(response == '0')
     {
         alert('Login Failed !!! Try Again');
     }

    },
    error: function(jqXHR, textStatus, errorThrown) {
       console.log(textStatus, errorThrown);
    }
});