简体   繁体   English

无法在加载了AJAX(jQuery)的元素上执行功能

[英]Unable to execute functions on elements loaded with AJAX (jQuery)

i am using .prepend() to load data after i POST a form with .ajax(). 我使用.ajax()发布表单后,正在使用.prepend()加载数据。

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. 如果我直接将js文件链接到前置数据中,则该函数起作用,但是当我POST时,我开始乘以事件。

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! 谢谢!

create a function that encapsulates the jEditable logic like so: 创建一个封装jEditable逻辑的函数,如下所示:

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: 第一个方法是使其从blurbs链接的onclick .load语句进行回调,如下所示:

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

that will make sure it gets run the first time without being in the $(document).ready() function. 这样可以确保它不会在$(document).ready()函数中第一次运行。

the second place would be in the success function of your .ajax call for adding the blurb. 第二个位置是您的.ajax调用的成功功能,用于添加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. 我认为您也需要在loadBlurbs函数启动和运行时执行相同的操作。 that should keep everything running with the editable plugin without reloading the script 100 times. 这样就可以使所有内容都通过可编辑插件运行,而无需重新加载脚本100次。

did i make sense that time? 那时候我有意义吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM