我有一个问题,我的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(" "," ")
//value = value.replace("&","&")
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内容不断重复,我很感激任何帮助来解决这个问题
答案 0 :(得分:2)
用以下代码替换有问题的.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的两个元素上运行,依此类推。