drblhw5731 2016-04-15 12:36
浏览 75
已采纳

jQuery .load()函数只更改内容一次[重复]

This question already has an answer here:

I've searched for other similar questions, but no answer works for my problem.

Here is my jQuery:

$("i[id^='delete']").click(function() {
  $(".container.tim-container").load("getreservations.php");
});

And here is the php that generates the updated content:

$count = 0;

echo '<div class="container tim-container">';
echo "<div>";
echo '<h1 class="title">View Reservations</h1>';
echo "</div>";
echo '<div class="reservations">';

foreach ($noDupes as $facilities)
  {
    echo '<div class="col-sm-12 left-column">';
    echo '<h2 id="facility_name'.$count.'" class="facility-title">'.$facilities["facility_name"].'</h2>';
    echo '</div>';

    foreach ($facilities["dates"] as $dates => $dateValues)
    {
      echo '<div class="col-sm-3 left-column">';
      echo '<h4 id="facility_date'.$count.'" class="facility-title">'.$dateValues["date"].'</h4>';
      echo '</div>';

      echo '<div class="col-sm-9 right-column">';
      echo '<ul>';
      foreach($dateValues["timeslots"] as $timeslots => $timeslotValues)
      {
        echo '<p id="start_time'.$count.'" hidden>'.$timeslotValues["start_time"].'</p>';
        echo '<p id="end_time'.$count.'" hidden>'.$timeslotValues["end_time"].'</p>';

        echo '<li class="row time-slot text-align">';
        echo '<h4 class="display_inline">'.$timeslotValues["start_time"].':00 - '.$timeslotValues["end_time"].':00</h4><i id="delete'.$count.'" class="fa fa-trash-o"></i>';
        echo '</li>';
        $count++;
      }
      echo '</ul>';
      echo '</div>';
    }
  }

  echo '</div>';
  echo '</div>';

Even though it is not beautiful php code, it works. The problem is that the .load() function in jQuery only works once. Also, I've tried using .on('click', function(){}) on the <i> element as well. How can I make it work more than once?

</div>
  • 写回答

1条回答 默认 最新

  • duankanjian4642 2016-04-15 12:38
    关注

    Use event delegation. It is used to bind all matched elements existing now and dynamically created elements.

    Note : while using event delegation you should attach it to a static element. Here I am attaching it to document. You can bind it to any other static element. Binding it to document have some performance issue. I don't know which all elements are static in your page, so I used document

    $(document).on('click', "i[id^='delete']", function() {
        $(".container.tim-container").load("getreservations.php");
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计