Хорошо, это мой jQuery:
$.ajax({
type: "POST",
url: $(this).attr('href')
}).done(function( msg ) {
$(this).parent().closest('p').html('Image Removed');
});
Это мой HTML:
<div id="gallery">
<? foreach($galleryImages as $image) { ?>
<div id="<?php echo $image['id'] ?>" class="box">
<img src="<?php echo $fileHandler->get_thumb(__LISTING_GALLERY__.$image['image']) ?>" alt="Gallery Image" class="gallery-image" /><br />
<p class="text-right"><a href="<?php echo __BASE_URL__.'/installers/dash/delete_gallery.php?id='.$image['id'] ?>" class="confirmation"><i class="fa fa-times"></i> Delete</a></p>
</div>
<? } ?>
</div>
Ajax отлично работает, так как изменяется база данных. Однако выполненная функция работает неправильно.
Я предупредил $(this).parent().closest('p').html()
и будет предупреждать текущий html, который имеет div. Тем не менее, он не будет перезаписывать его с помощью "Image Removed", который я вводил.
this
внутри сделанной функции не является элементом, так как существует новая область функций
var elem = this;
$.ajax({
type: "POST",
url: $(this).attr('href')
}).done(function( msg ) { // new function scope, "this" is now the XHR function
$(elem).parent().closest('p').html('Image Removed');
});
Используйте параметр контекста ajax:
$.ajax({
type: "POST",
url: $(this).attr('href'),
context:this
}).done(function( msg ) {
$(this).parent().closest('p').html('Image Removed');
});