简体   繁体   English

jQuery脚本在.load()之后不起作用

[英]jquery script doesnt work after .load()

Im just started yesterday with jquery/ajax. 我昨天刚开始使用jquery / ajax。 And im trying some things out, but I run into a wall. 我正在尝试一些事情,但是我碰壁了。

I have an index file where I include my js script in this script I have two functions (see below) 我有一个索引文件,在其中包含我的js脚本,我有两个功能(请参见下文)

In the index file I used include to include a php file that contains a table with some rows (rows from mysql). 在索引文件中,我使用了包括一个php文件,该文件包含带有一些行的表(来自mysql的行)。 This included file I also use for the .load in my js script (mod/hoofdmenu.php). 这个包含的文件也用于我的js脚本(mod / hoofdmenu.php)中的.load。

One script is for changing the order of the rows by drag and drop, this works perfect. 一个脚本用于通过拖放来更改行的顺序,这很完美。 Other script is for changing 1 row when you click a image (publish/unpublish). 其他脚本用于在单击图像(发布/取消发布)时更改1行。 This script works also perfect. 这个脚本也很完美。 BUT when I run this script the first script (drag and drop) doenst work anymore. 但是,当我运行此脚本时,第一个脚本(拖放)不再起作用。 The 'publish/unpublish' script still works. “发布/取消发布”脚本仍然有效。

I think its because the $('#showdata').load('mod/hoofdmenu.php'); 我认为是因为$('#showdata')。load('mod / hoofdmenu.php'); All advise/help is very much appreciated. 非常感谢所有的建议/帮助。

the includes js file: 包含js文件:

$(function() {
    $(".entry").live("click",function(event) {
    event.preventDefault();
    var id = $(this).attr("id");
    var dataString = 'id='+ id ;
    var parent = $(this).parent();
    $.ajax({
            url: "save.php",    
            type: "POST",       
            data: dataString,       
            cache: false,
            success: function (html) {  
                $('#showdata').load('mod/hoofdmenu.php');           
            }       
        });
    });
});
$(function() {
    $(".tbl_repeat tbody").tableDnD({
        onDrop: function(table, row) {
            var orders = $.tableDnD.serialize();
            $.post('mod/order.php', { orders : orders });
        }
    });

});

The drag and drop event applied to a DOM element that has now been replaced with the new stuff loaded by the AJAX request. 应用于DOM元素的拖放事件现在已被AJAX请求加载的新内容替换。 You need to put the tableDnD behaviour on to the new elements. 您需要将tableDnD行为放在新元素上。

There may be some fancy livequery style way of doing this but this should work: 可能有一些花哨的livequery风格的方式可以做到这一点,但这应该可行:

function initDnD(root) { 
    root.find(".tbl_repeat tbody").tableDnD({
        onDrop: function(table, row) {
            var orders = $.tableDnD.serialize();
            $.post('mod/order.php', { orders : orders });
        }
    });
};

$(function() {
    $(".entry").live("click",function(event) {
        event.preventDefault();
        var id = $(this).attr("id");
        var dataString = 'id='+ id ;
        var parent = $(this).parent();
        $.ajax({
            url: "save.php",    
            type: "POST",       
            data: dataString,       
            cache: false,
            success: function (html) {  
                $('#showdata').load('mod/hoofdmenu.php', function() {
                    initDnD($('#showdata'));
                });           
            }       
        });
    });
    initDnD($("body"));
});

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

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