douguxun6866 2013-04-10 18:51
浏览 46
已采纳

模态窗口在同一查询实例上多次打开

I think I just need to re-work this code. Here is my problem.

I am calling a query to show a list of "offers" underneath Categories. These links are then clicked and they are opening in a modal box from jquery.

Everything works great! However, there are some offers that are the same underneath different categories ( same unique ID ).

When more than one offer link is on the page, and if you click it, it will load the modal window twice.. because there are more than one instance on that page.

I know what the problem is , but I can't get my head around how to code it correctly! I know I don't need to create or call the script every time in my query ??

Thanks, here is my query code:

$line .= '<span class="icon-caret-right"></span> <a id="manual-ajax'.$offer_id.'" href="view.php?o='.$offer_id.'">'.$offer_title.'</a><br />';  


$line .= " <script> $('#manual-ajax$offer_id').click(function(event) {
              event.preventDefault();
              $.get(this.href, function(html) {
              $(html).appendTo('body').modal();

      });
    });

</script>";
  • 写回答

1条回答 默认 最新

  • dtkz3186 2013-04-10 19:07
    关注

    The problem is that your IDs aren't unique, as they're required to be. So when the same ID reappeared, you were binding additional click handlers to the elements.

    But you don't need IDs if you use a class instead.

    $line .= '<span class="icon-caret-right"></span> <a class="manual-ajax" href="view.php?o='.$offer_id.'">'.$offer_title.'</a><br />';
    

    then write a single handler:

    $(function() {
        $(".manual-ajax").click(function(event) {
            event.preventDefault();
            $.get(this.href, function(html) {
                $(html).appendTo('body').modal();
            });
        });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看