Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将事件绑定到ajax生成的元素_Ajax_Yii_Jquery - Fatal编程技术网

如何将事件绑定到ajax生成的元素

如何将事件绑定到ajax生成的元素,ajax,yii,jquery,Ajax,Yii,Jquery,我正在使用RenderPartial生成CListView,所有生成的内容都正确,分页效果也很好。但是,当我将自定义JS添加到由CListview生成的元素中时,它对第一页内容的效果很好,但当我使用分页并单击第2页时,JS绑定失败 有没有其他方法可以将自定义事件绑定到YIICListview中生成的元素,我已经尝试过使用live,这里是我的js文件 我想我必须在中的每个ajax加载上调用我的函数,但是如何在yii中实现呢 这是我用来通过单击按钮更新服务器评级的脚本,这些表单和按钮的定义元素由yi

我正在使用
RenderPartial
生成
CListView
,所有生成的内容都正确,分页效果也很好。但是,当我将自定义JS添加到由
CListview
生成的元素中时,它对第一页内容的效果很好,但当我使用分页并单击第2页时,JS绑定失败

有没有其他方法可以将自定义事件绑定到YII
CListview
中生成的元素,我已经尝试过使用live,这里是我的js文件

我想我必须在中的每个ajax加载上调用我的函数,但是如何在yii中实现呢

这是我用来通过单击按钮更新服务器评级的脚本,这些表单和按钮的定义元素由yii中的
CListview
生成

$(document).ready(function(){
    $('form[id^="rating_formup_"]').each(function() {
        $(this).live('click', function() {
            alert("hi");
            var profileid=  $(this).find('#profile_id').attr('value');
            var userid=  $(this).find('#user_id').attr('value');  
            console.log(userid);
            var data = new Object();
            data.profile_id=profileid;
            data.user_id=userid;
            data.liked="Liked";
            $.post('profile_rating_ajax.php', data, handleAjaxResponse);
            return false;     
       });
    });
});

这个问题可以通过两种方式解决:

  • 对将要接收该事件的每个项目使用“onclick”html定义,并且在生成元素时,将$data的id传递给js函数。例如,在“查看”页面内:

    echo CHtml::htmlButton('click me', array('onclick'=>'myFunction('.$data->id.')');
    
  • 像框架一样将事件处理程序绑定到“body”。在ajax更新之后,它们将继续存在:

    $('body').on('click','#myUniqueId',funcion(){...});
    
  • 您也可以尝试:


    $。每个方法将只在现有元素上循环,因此live binder将永远看不到ajax生成的内容

    你为什么不这样试试呢:

    $(document).ready(function(){
      $('form[id^="rating_formup_"]').live('click', function() {
          alert("hi");
          var profileid=  $(this).find('#profile_id').attr('value');
          var userid=  $(this).find('#user_id').attr('value');  
          console.log(userid);
          var data = new Object();
          data.profile_id=profileid;
          data.user_id=userid;
          data.liked="Liked";
          $.post('profile_rating_ajax.php', data, handleAjaxResponse);
          return false;     
       });
    });
    

    自jQuery 1.7起,
    .live
    已被弃用。使用
    .on
    ,因为我尝试的替换和live两者似乎都不起作用。如果部分渲染,请确保对HTML进行后处理。的第四个参数应该是true。在几个错误的开始之后,我想明确说明“applyEventHandlers(id,data)”是一个必须编写的函数。它不是CGridView对象的内置函数,您可以使用它将事件处理程序重新绑定到封闭的元素。
    $(document).ready(function(){
      $('form[id^="rating_formup_"]').live('click', function() {
          alert("hi");
          var profileid=  $(this).find('#profile_id').attr('value');
          var userid=  $(this).find('#user_id').attr('value');  
          console.log(userid);
          var data = new Object();
          data.profile_id=profileid;
          data.user_id=userid;
          data.liked="Liked";
          $.post('profile_rating_ajax.php', data, handleAjaxResponse);
          return false;     
       });
    });