duandiaoqian5795 2010-06-05 00:41
浏览 45
已采纳

无法在使用AJAX(jQuery)加载的元素上执行函数

i am using .prepend() to load data after i POST a form with .ajax().

once the new elements are added, my functions won't work on them.

if i link the js file directly in the prepended data, the functions work, but when i POST, i starts multiplying the events.

i have a feeling it has something to do with binding, but i am not able to figure out exactly how to handle it.

any ideas?

thanks!

  • 写回答

1条回答 默认 最新

  • donglefu6195 2010-06-05 00:58
    关注

    create a function that encapsulates the jEditable logic like so:

    function initjEditable(){
        $('.review_edit').editable('edit_review.php', {
             indicator : 'Saving...',
             tooltip   : 'Click to edit...'
        });
    
        $('.review_edit_area').editable('edit_review.php', { 
             type      : 'textarea',
             cancel    : 'Cancel',
             submit    : 'OK',
             indicator : '<img src="/hrt/images/indicator.gif">',
             tooltip   : 'Click to edit...'
        });
    }
    

    then call that function from a couple of places. the first would be to make it a callback from the blurbs link's onclick .load statement like so:

    $('#adminContainer').load('blurbs.php',function(){ initjEditable(); });
    

    that will make sure it gets run the first time without being in the $(document).ready() function.

    the second place would be in the success function of your .ajax call for adding the blurb. would look like this:

    $.ajax({
        type: 'POST',
        url: "add_review.php",
        data: $(this).parent().serialize(),
        dataType: "html",
        success: function(data){
            $("#results").prepend(data);
            initjEditable();
            $('#addForm').reset();
            $('#add').hide('fast');
        }
    });
    

    i assume you'd need to do the same sort of thing with the loadBlurbs function when it is up and running too. that should keep everything running with the editable plugin without reloading the script 100 times.

    did i make sense that time?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。