jquery ajax给所有div都给出了相同的结果

我试图能够重命名文件。 在其工作的时刻,但例如,让我说我有:

文件名是’hello’,文件名是’bye’,文件三名是’嘿’

我将文件一重命名为’something’它将重命名它,但如果我然后将文件二重命名为’youarecool’它将重命名文件一和二。

这是我的javascript,我做错了什么。 这是我的jsfiddle。 http://jsfiddle.net/RSvre/

这是每个文件的html

filesize | downloads Downloads | Uploaded on date

这是javascript

 $('.edit').click(function(e){ e.preventDefault(); var auth = $(this).attr('id'); var each = $(this).closest('.each_file').find('.fnl'); $.post( "ajax/edit_filename.php", { auth:auth }) .done(function( data ) { $( "#dialog" ).dialog({ modal: true, resizable: false, title: 'Edit file name', buttons: { "Close": function() { $(this).dialog("destroy"); $(this).dialog("close"); } } }); $('.ui-dialog-content').html('
'); }); $(document).on('click', '#edit_filenameb', function(e){ e.preventDefault(); var nname = $('.newfname').val(); console.log(nname); if(nname == ''){ $('.submit_btn').effect('shake'); } else { $.post('ajax/change_filename.php', {nname:nname, auth:auth}) .done(function(data){ each.text(data); $('#dialog').dialog('close'); }); } }); });

我相信问题在于,每次重命名文件时,都会向document添加一个新的单击侦听器,但是您从不清理它。 当您重命名下一个时,它实际上调用了两个重命名函数。 您可以在变量中保存要重命名的文件的名称,然后在页面上的单击侦听器集中重命名该文件。 此外,您还必须有一种方法来判断重命名是否已经发生,以便在每次点击时都不会调用它。

你必须包装你的内部点击绑定,所以你有:

 $('.edit').click(function(e){ e.preventDefault(); var auth = $(this).attr('id'); var each = $(this).closest('.each_file').find('.fnl'); $.post( "ajax/edit_filename.php", { auth:auth }) .done(function( data ) { $( "#dialog" ).dialog({ modal: true, resizable: false, title: 'Edit file name', buttons: { "Close": function() { $(this).dialog("destroy"); $(this).dialog("close"); } } }); $('.ui-dialog-content').html('
'); }); //Code from here took to outside }); //This was taken out from the other click binding $(document).on('click', '#edit_filenameb', function(e){ e.preventDefault(); var nname = $('.newfname').val(); var auth = ... //Get the auth here console.log(nname); if(nname == ''){ $('.submit_btn').effect('shake'); } else { $.post('ajax/change_filename.php', {nname:nname, auth:auth}) .done(function(data){ each.text(data); $('#dialog').dialog('close'); }); } });

希望这可以帮助。 干杯