2012-10-01 107 views
0

我正在使用下面的代码来检查名称是否存在。 但是,我无法获得返回值是否为真,以进一步验证。我怎样才能从这个函数获得返回值?从嵌套的jquery函数返回一个值

function check_availability() //check auction name if exists 
     { 
     var new_Auction =$('#txtAuction').val(); 
     $.post('checkAuction.php',{txtAuc:$('#txtAuction').val()}, 
      function(result){ 
       if(new_Auction.length==0) 
       { 
        $('#message').html(''); 
       } 
       else 
       { 
       if(result==1) 
       { 
        $('#message').html(new_Auction + ' is Available').css('color','#0C3'); 
        $('input[id=btnCreate]').removeAttr('Disabled'); 
       //$('h4.alert_success').css("display","block"); 
       //$('h4.alert_success').html(new_Auction + ' is Available'); 
       //$('h4.alert_success').fadeOut(5000); 
       return true; 

       } 
       else 
       { 

        $('#message').html(new_Auction + ' is not Available').css('color','#F00'); 
        $('input[id=btnCreate]').attr('Disabled','Disabled'); 
        //$('h4.alert_error').css("display","block"); 
        //$('h4.alert_error').html(new_Auction + ' is not available'); 
        return false; 
       } 
       } 
      } 
     ); 
     } 

回答

0

AJAX是异步。您不能从回调返回到异步调用。将代码依赖于该AJAX调用的结果,而不是尝试从该回调中返回。

$.post('checkAuction.php', {txtAuc:$('#txtAuction').val()}, function(result){ 
    //You cannot return a value from in here! 
}); 

当你做一个异步请求,继续执行下一条语句,所以就无处可控制“重返”了一段时间后,当它执行异步回调。

+0

那么有什么办法可以得到验证与其他jQuery返回功能一起吗? – user1657720