【问题标题】:Jquery Ajax: Refreshing multiple divs with different id'sJquery Ajax:刷新具有不同ID的多个div
【发布时间】:2015-02-14 17:13:06
【问题描述】:

我想刷新页面上多个项目的倒计时。每个计时器都有“刷新项目”类。它只适用于一个项目,但如果我有多个项目(这也意味着不同的倒数计时器),它似乎只选择最新的 id。

这是我的 JQuery 代码:

function timeLeft()
{   
    var data = $(".refresh-item").attr("data-content"); 
    var dataString = 'case=refresh_item&' + data;

    $.ajax({
      type: "GET",
      url: "ajax.php",
      dataType: "html",
      data: dataString,
      success: function(result) 
        {   
          $(".refresh-item").html(result);
        }
    });
}

window.setInterval(function(){
    timeLeft();
}, 1000);

每个项目还具有“数据内容”属性,其中保存了项目的特定 ID。如何选择特定项目的特定 ID(然后显示单个倒数计时器)?

【问题讨论】:

标签: javascript jquery html ajax


【解决方案1】:

在jQuery中使用each()方法进行迭代

function timeLeft() {
  $(".refresh-item").each(function() {
    $this = $(this);
    var data = $this.attr("data-content");
    var dataString = 'case=refresh_item&' + data;
    $.ajax({
      type: "GET",
      url: "ajax.php",
      dataType: "html",
      data: dataString,
      success: function(result) {
        $this.html(result);
      }
    });
  });
}

window.setInterval(function() {
  timeLeft();
}, 1000);

【讨论】:

    【解决方案2】:

    使用jquery的$.each函数:

    function timeLeft()
    {   
        $.each($(".refresh-item"), function(){
            $this = $(this);
            var data = $this.attr("data-content"); 
            var dataString = 'case=refresh_item&' + data;
             $.ajax({
              type: "GET",
              url: "ajax.php",
              dataType: "html",
              data: dataString,
              success: function(result) 
                {   
                  $this.html(result);
                },
              complete: function(){}
        });
        )
    
    }
    
    window.setInterval(function(){
        timeLeft();
    }, 1000);
    

    【讨论】:

    • in complete function in ajax request call next request: complete: function(){ }
    猜你喜欢
    • 1970-01-01
    • 2015-06-24
    • 1970-01-01
    • 1970-01-01
    • 2013-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-02
    相关资源
    最近更新 更多