dse55384 2015-02-20 04:39
浏览 103
已采纳

如何从通过ajax加载的PHP页面调用jquery方法

I have a page where I am loading a series of buttons into a div using jquery/ajax. This is a script for loading every time the user user scrolls the page. Each of the buttons are clickable and run a jquery function after being clicked. The problem is when I switched to loading the buttons from a different page the now don't call any method. If I switch the buttons to calling a pure javascript function it works just fine but I need the button to call a jquery function as the rest of the script, which is quite long, has been done in jquery.

Here is what I am talking about: Page A:

             $(document).ready(function() {
                 var track_load = 0; //total loaded record group(s)
                 var loading  = false; //to prevents multipal ajax loads
                 var total_groups = '<?php echo $total_groups; ?>';

              $('#results').load("testshareload.php", {'group_no':track_load}, function(result) {
                                 track_load++;
                                 }); //load first group


              $(window).scroll(function() { //detect page scroll
              });

              $('#testerino').on('click', function () {
                    console.log("CALLED");
              });

PAGE B: (testshareload.php)

<?php
    echo "<input type='button' value='Button' id='testerino'>";
?>

It also will not work for me to do this due to the existing code:

function testerino() {
      $(document).ready(function() {
      });
}

What else can I do to solve this problem?

  • 写回答

2条回答 默认 最新

  • dt3999 2015-02-20 04:52
    关注

    You're creating a click handler for an element that doesn't exist, and this will fail in jQuery. What you need to do is create a DELEGATED handler for the item:

    $(document).on('click','#testerino', function () {
        console.log("CALLED");
    });
    

    The document accepts the handler being registered, and later when it gets a click event from an element matching the selector #testerino it will fire off the handler.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料