javascript
  • jquery
  • ajax
  • 2016-07-07 80 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()'請求中對同一位置進行不必要的重複調用 –

    相關問題