![](/img/trans.png)
[英]Multiple Image upload with previw and delete , Jquery and Javascript
[英]Jquery delete button is selecting all image srcs and ids (Multiple Upload)
我正在尝试为ImageUploader创建一个删除按钮。 选择图片并将其放在页面的div元素中没有任何问题,这全都与删除当前图片有关。
当我点击删除按钮时,它会给我所有的ID和src,而不是带有删除按钮的当前当前ID。 看看我的删除按钮,我在哪里做控制台日志src和id。 它给了我所有的ID和src,我确实想要当前ID和src。
有人有解决方案吗?
这是完美的选择作品。
frame.on( 'select', function() {
// Get media attachments details from the frame state
selections = frame.state().get('selection');
selections.map(function(attachment){
attachment = attachment.toJSON();
// Send the attachment URL to our custom image input field
imgContainer.append(
'<li>'
+ '<img data-attachment-id="id-media-1993'+attachment.id+'" src="'+attachment.url+'" class="gallery-thumbnail" alt="'+attachment.title+'" style="max-width:150px; max-height:150px;"/>'
+ '<a class="delete-custom-img" href="#">Remove Image</a>'
+ '</li>');
// Send the attachment id to our hidden input
imgIdInput.val(attachment.id);
console.log(attachment);
});
});
// Finally, open the modal on click
frame.open();
});
这是我的删除按钮
imgContainer.on( 'click', delImgLink, function(event){
event.preventDefault();
var galleryThumbnail = $('.gallery-thumbnail');
var galleryThumbnailID = $('.gallery-thumbnail').data('attachment-id');
var galleryThumbnailSrc = $('.gallery-thumbnail').attr('src');
$(galleryThumbnail).each(function(){
var imgSrc = $(this).attr('src');
console.log(imgSrc);
});
$(galleryThumbnail).each(function(){
var imgIDs = $(this).data("attachment-id");
console.log(imgIDs);
});
});
您可以从按钮中选择父元素,然后从那里查找要查找的内容。
像这样的东西
var img = $(this).closest('li').find('.gallery-thumbnail');
var galleryThumbnail = img;
var galleryThumbnailID = img.data('attachment-id');
var galleryThumbnailSrc = img.attr('src');
起初,我认为添加这样的事件处理程序会减少混乱:
$('.delete-custom-img').click(function() {...});
要么
$('.delete-custom-img', imgContainer).click(function() {...});
如果您不想将事件处理程序添加到imgContainer之外,则此类具有其他元素。
但这是个人喜好,所以我想提一个问题:问题是您在页面上出现了所有的'.gallery-thumbnail',因为您没有指定要查找的jQuery范围(例如上面的imgContainer) 。 因此,单击删除按钮时,它就在删除按钮的范围内。 在您生成的标记中,它与缩略图共享同一父对象,因此您可以执行以下操作:
var galleryThumbnail = $('.gallery-thumbnail', $(this).parent());
第二个参数指定jQuery搜索带有'.gallery-thumbnail'类的元素的范围。
还没有测试过,但是我很确定这可以解决您的问题。
干杯,莫
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.