5

我想false从完成$.ajax时返回success

$.ajax({
    url: '' + $website_url + 'queries/voorraad_berekenen.php',
    type: 'post',
    data: {
        aantal: $(this).parent('form').children('.quantity').val(),
        item_number_1: $(this).parent('form').children('.item_number_1').val()
    },
    success: function(result) {
        return false;
    }
});

这行不通。有解决办法吗?

4

5 回答 5

12

从您的帖子中,我猜您调用了一个包含该函数的函数$.ajax()并尝试调用return false该函数。但你不能这样做,因为AJAX是异步的。

最好从 ajax 成功函数中调用一个函数,如下所示:

$.ajax({
    url: '' + $website_url + 'queries/voorraad_berekenen.php',
    type: 'post',
    data: {
        aantal: $(this).parent('form').children('.quantity').val(),
        item_number_1: $(this).parent('form').children('.item_number_1').val()
    },
    success: function(result) {
        var returned = true;
        if(some_condition_not_satisfied) {
          returned = false;
        } else {

        }
        // call a function
        calledFromAjaxSuccess(returned);
    }
});

function calledFromAjaxSuccess(result) {
  if(result) {
    alert('TRUE');
  } else {
    alert('FALSE');
  }
}
于 2012-05-26T14:50:53.793 回答
1

也许您可以尝试这样的事情(值 1 和 0 应该更改为您使用的值):

success: function(result){
if(result === '1'){
// do something
}
else
   return false;
}
于 2012-05-26T14:44:44.420 回答
0

只需使用 asunc false 它可以正常工作我已经实现了

就试一试吧

$.ajax({
    url: '' + $website_url + 'queries/voorraad_berekenen.php',
    type: 'post',
    data: {
        aantal: $(this).parent('form').children('.quantity').val(),
        item_number_1: $(this).parent('form').children('.item_number_1').val()
    },
   async: false, //=>>>>>>>>>>> here >>>>>>>>>>>
    success: function(result) {
        return false;
    }
});

它工作正常尝试一次

于 2014-07-07T07:11:15.530 回答
0
<form action="yourpage" method="post" onsubmit="return matchpass();">
  <div>
   <label> Name</label>
   <input type="text" name="name" id="name">
  </div>
  <div>
   <label> Email ID</label>
   <input type="email" name="email" id="email">            
  </div>
  <div>
   <label> Mobile No</label>
   <input type="text" name="mob"  maxlength="10" onkeyup="check_if_exists();" autocomplete="off" id="mob">
  </div>                             
  <div>
   <button type="button" >Send</button>
  </div>
  <span id="err"></span>
  <div>
   <label> OTP</label>
   <input type="password" name="otp" id="otp"  maxlength="6" placeholder="****">
   <span id="err2"></span>
  </div>    
  <div>
   <input type="reset" value="Reset" class="reset-btn">
   <input type="submit" name="submit" id="submit" value="Submit" >              
  </div>
</form>
<input type="hidden"  id="otpcheck"/>



<script>
function matchpass()
{

     $.ajax({   
    type:"post",
    url: "yourpage",
    data:{ mobile:mob,otp:otp},
    success:function(data)
    {
       if(data==1)
       {
           document.getElementById("otpcheck").value=1; //important
       }else{

           document.getElementById("err2").style.color = "red";
           document.getElementById("err2").innerHTML = "invalid OTP Number ";
         document.getElementById("otpcheck").value=0; //important
       }


    }
    });

    if(document.getElementById("otpcheck").value==0){
        return false;
    }

}

于 2018-12-26T07:14:58.133 回答
-1

我面临这个问题。然后我找到了实际的解决方案。使用 async : false,在 ajax 调用中

function thisisavailavailusername(uname){
var dataString = 'username='+ uname.value;
var trueorfalse = false;
$.ajax({
    type: "post",
    url: "/BloodBook/checkavailableusername",
    data: dataString,
    cache: false,
    async : false, //user this then everything ok
    success: function(html){
        if(html=="true"){
            $('#validusername').html('Available');
            trueorfalse = true;
        }else{
            $('#validusername').html('Not Available');
            trueorfalse = false;
        }

    },
    error: function() {
        alert("Something Wrong...");
    }
});

return trueorfalse;

}

于 2016-10-15T17:44:17.093 回答