javascript
  • jquery
  • ajax
  • 2016-07-07 81 views 0 likes 
    0

    我想提出一个Ajax调用,它看起来像这样:获取阿贾克斯的价值变化领域

    $.ajax({ 
        url: "index.php?id=70&type=999", 
        type: 'post', 
        data: form + '&sort=' + val, 
        success: function(response) 
          { 
          $(".listing").load(location.href + " .listing"); 
          $(".count").load(location.href + " .count"); 
          }, 
          complete: function (response) { 
           alert($(".count").val()); 
          }, 
         error: function(xhr, ajaxOptions, thrownError){ 
          alert(xhr.status); 
         } 
    }); 
    

    所以,你可以看到2班“上市”和“计数”正在发生变化,这工作正常。在我试图提醒“count”类的NEW值之后,它总是给我一个实际的值。紧接着警报之后,值发生变化。但是complete:是不是表示该呼叫是在success:函数之后发出的?为什么警报是先做出来的,并在ajax调用之前为我提供旧值?内成功

    回答

    1

    试着改变你这样的代码:

    $.ajax({ 
            url: "index.php?id=70&type=999", 
            type: 'post', 
            data: form + '&sort=' + val, 
            success: function(response) 
            { 
             $(".listing").load(location.href + " .listing"); 
             $(".count").load(location.href + " .count", function(){ 
               alert($(".count").val()); 
             }); 
            }, 
            complete: function (data) { 
    
            }, 
            error: function(xhr, ajaxOptions, thrownError){ 
             alert(xhr.status); 
            } 
           }); 
    
    +0

    这是工作,非常感谢。 –

    +0

    很高兴帮助:) – Roxoradev

    1

    警戒值:

    $.ajax({ 
        url: "index.php?id=70&type=999", 
        type: 'post', 
        data: form + '&sort=' + val, 
        success: function(response) 
        { 
         $(".listing").load(location.href + " .listing"); 
         $(".count").load(location.href + " .count", function(){ 
          alert($(".count").val()); 
         }); 
         }, 
         error: function(xhr, ajaxOptions, thrownError){ 
         alert(xhr.status); 
         } 
    }); 
    
    1

    问题是因为complete火灾第一 AJAX请求完成后,没有经过通过调用load()创建的第二个/第三个AJAX请求。如果你想知道.count价值,你需要把一个回调的load()方法:

    $.ajax({ 
        url: "index.php?id=70&type=999", 
        type: 'post', 
        data: form + '&sort=' + val, 
        success: function(response) { 
         $(".listing").load(location.href + " .listing"); 
         $(".count").load(location.href + " .count", function() { 
          // get the value here, after load() completes 
          console.log($(".count").val()); 
         }); 
        }, 
        error: function(xhr, ajaxOptions, thrownError){ 
         alert(xhr.status); 
        } 
    }); 
    

    还要注意的是,你可以通过调用两个load()呼叫URL只是一次,然后提取所需信息,提高了代码从响应,像这样:

    $.ajax({ 
        url: "index.php?id=70&type=999", 
        type: 'post', 
        data: form + '&sort=' + val, 
        success: function(response) { 
         $.ajax({ 
          url: location.href, 
          success: function(html) { 
           $(".listing").html($(html).find('.listing')); 
           $(".count").html($(html).find('.count')); 
          } 
         }); 
        }, 
        error: function(xhr, ajaxOptions, thrownError){ 
         alert(xhr.status); 
        } 
    }); 
    
    +0

    Roxoradev给了我同样的建议,它是这样工作的。感谢你们! –

    +0

    没问题,很乐意帮忙。请注意,我只是更新了我的答案,以避免在2'load()'请求中对同一位置进行不必要的重复调用 –

    相关问题