0

我在这里有一个问题,我的 jquery .load 函数似乎运行了两次,即使我只调用了一次。这是我的代码:

function updateRowItem(idIn){
if (ready) {
    id = idIn
    $("#controlPanelResult").css("background-color", "");
    var query = {};

    var tid = id.replace("@", "\\@");
    tid = tid.replace(".", "\\.");

    var value =  $('#'+tid).text();

    //var value =  decodeURI(document.getElementById(id).innerHTML);

    //if (value == "<br>"){
    //  value = "NULL"
    //}
    //value = value.replace("&nbsp;"," ")
    //value = value.replace("&amp;","&")

    var split = id.split('@');
    var temp = split[0];
    var uuid = split[1];
    var temp2 = temp.split('.');
    var tableName = temp2[0];
    var item = temp2[1];

    query['table_name'] = tableName;
    query['uuid'] = uuid;
    query[item] = value;

    var tid = id.replace("@", "\\@");
    tid = tid.replace(".", "\\.");

    $.ajax({
    async: false,
    type: "POST",
    //url: '/admin/updateRow?table_name=' + tableName + "&uuid=" + uuid +"&" +     item + "=" + value,
    url: '/admin/updateRow',
    data: query,
    success: function(data) {
            if (data == "1"){ 

                alert ("Hellooo!")//This alert only and always pops up once

                    $('#'+tid).empty().load(showRowsUrl + " #" + tid,function (status) { 
                        alert (status) // This alert pops up first once, then twice, then four times, then eight etc....

                        $("#"+tid).animate({backgroundColor: '#70DB70'}, 'slow');
                        $('#controlPanelResult').html("Updated: " + id);
                        $("#controlPanelResult").animate({ backgroundColor: '#70DB70'}, 'slow');
                        $("#"+tid).animate({backgroundColor: 'white'}, 'slow');
                    });

            } else {
                $('#mainRows').load(showRowsUrl, function() {
                    $("#"+tid).animate({backgroundColor: 'red'}, 'fast');
                    $('#controlPanelResult').html(data);
                    $("#controlPanelResult").animate({backgroundColor: 'red'}, 'fast'); 
                });
            }               
        }
    });
}
}

这会导致我的 div 内容不断重复,我将不胜感激任何帮助解决这个问题

4

1 回答 1

3

用这个替换有问题的.load()调用:

$.get(showRowsUrl, function(data) {
    $('#'+tid).replaceWith($('#'+tid,'<div>'+data+'</div>'));

    $("#"+tid).animate({backgroundColor: '#70DB70'}, 'slow');
    $('#controlPanelResult').html("Updated: " + id);
    $("#controlPanelResult").animate({ backgroundColor: '#70DB70'}, 'slow');
    $("#"+tid).animate({backgroundColor: 'white'}, 'slow');
});

解释:.load正在将元素加载到自身中,因此之后会有两个具有相同 ID 的元素嵌套。然后当您.load再次运行时,它会在具有该 ID 的两个元素上运行,依此类推。

于 2012-06-30T20:47:34.423 回答