我想在单击$(.excluir)时刷新一个表。该表是从php数据加载的。当我点击按钮时,php代码就会执行,我会从数据库中删除数据。但是我想刷新表的内容。因此,我尝试再次调用函数completeTable();
,但它不起作用。将出现警报,但表不会刷新。
function completeTable(){
$.post("completa_tabela.php", {cd_turma: $('#turma').val()}, function(data){
data = jQuery.parseJSON(data);
$.each(data.json, function(){
var button = "<button type='button' class='excluir' value='" + this['codigo'] + "'>X</button>";
$('#' + this['dia_hora']).html(this['nome'] + button);
});
$('.excluir').click(function(){
$.post("altera_horario.php", {excluir: true, cd_horario: $(this).val()}, function(){
alert("$('excluir').click");
completeTable();
});
});
});
}
我该怎么做呢?
发布于 2014-02-07 04:56:31
您的函数不是递归函数。在您的代码中,您将动态创建元素并绑定事件。
您可以使用。您应该使用委托事件方法来使用。
委托事件的优点在于,它们可以处理稍后添加到文档中的后代元素中的事件。
将您的代码更改为
function completeTable(){
$.post("completa_tabela.php", {cd_turma: $('#turma').val()}, function(data){
data = jQuery.parseJSON(data);
$.each(data.json, function(){
var button = "<button type='button' class='excluir' value='" + this['codigo'] + "'>X</button>";
$('#' + this['dia_hora']).html(this['nome'] + button);
});
});
}
$(document).on('click','.excluir', function(){
$.post("altera_horario.php", {excluir: true, cd_horario: $(this).val()}, function(){
alert("$('excluir').click");
completeTable();
});
});
发布于 2014-02-07 04:53:44
请使用$('.excluir').bind('click', function(){});
而不是$('.excluir').click(function(){});
您也可以使用$('.excluir').live('click', function(){});
https://stackoverflow.com/questions/21613744
复制相似问题