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 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多