我有下面的標記查找最近的圖像元素和刷新圖像源
<div class="col-md-4">
<div class="row">
<img name="previewImage" alt="" src="" style="max-height:300px;max-width :350px;">
</div>
<div class="row">
<button name="btnPushPhoto" type="button" class="btn btn-default" tabindex="-1">Push</button>
<button name="btnRemovePhoto" type="button" class="btn btn-default" tabindex="-1">Remove</button>
</div>
</div>
當我點擊刪除按鈕的照片,我能夠把我的形象爲空(在服務器端),但是在客戶端,我在定位圖片標籤時遇到了一些困難,並對其進行了刷新。
$(document).on("click", "[name=btnRemovePhoto]", function() {
var r = confirm("Are you sure you want to remove the photo?");
if (r == true) {
var uniqueId = $(this).data('uniqueId');
$.post($("#remove-photo-url").val(), { uniqueId: uniqueId })
.done(function() {
//var img = $(this).closest('[name=previewImage]');
var img = $(this).parent().parent().find('[name=previewImage]');
console.log(img);
img.attr('src', '');
img.attr('src', img.attr('src') + '?' + Math.random());
});
}
});
這裏沒有什麼事情發生。瀏覽器的控制檯沒有錯誤。
這是console.log(img);
[prevObject: x.fn.x.init[0], context: undefined, selector: "[name=previewImage]"]
什麼的console.log(IMG)的輸出? – DoctorMick
屬性選擇器需要[name ='btnRemovePhoto'](在這種情況下,單引號) https://api.jquery.com/attribute-equals-selector/ – MaxRocket
'this' inside'done()'callback指jqXHR選項對象 –