Jquery删除按钮正在选择所有图像srcs和id(多次上传)

时间:2018-03-06 11:54:59

标签: javascript jquery html

我正在尝试为ImageUploader创建一个删除按钮。&#xA;我选择图像并将其放在我页面的div元素中没有任何问题,这都是关于删除当前图像。< / p>&#xA;&#xA;

当我点击我的删除按钮时,它会使用我的删除按钮给出我所有的id和src,而不是所选的当前ID。看看我的删除按钮请在哪里我做控制台记录src和id。它给了我所有的id和src,我确实想要当前的id和src。

&#xA;&#xA;

有人有解决方案吗?

&#xA; &#xA;

这是完美的选择。

&#xA;&#xA;
  frame.on('select',function (){&#xA; //从帧状态获取媒体附件详细信息&#xA; selections = frame.state()。get('selection');&#xA; selections.map(function(attachment){&# xA; attachment = attachment.toJSON();&#xA;&#xA; //将附件网址发送到我们的自定义图片输入字段&#xA; imgContainer.append(&#xA;'&lt; li&gt;'&#xA ; +'&lt; 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;“/&gt;'&#xA; +'&lt; a class =”delete-custom-img“href =”#“&gt;删除图像&lt; / a&gt;'&#xA; +'&lt; / li&gt;');&#xA; //发送t他依附于我们隐藏的输入&#xA; imgIdInput.val(attachment.id);&#XA;&#XA;的console.log(附着);&#XA; });&#XA; });&#XA;&#XA; //最后,点击&#xA打开模态; frame.open();&#xA;&#xA;});&#xA;  
&#xA;&#xA;

这是我的删除按钮

&#xA;&#xA;
  imgContainer.on('click',delImgLink,function(event){&#xA; event.preventDefault();&#xA; var galleryThumbnail = $('。gallery-thumbnail');&#xA; var galleryThumbnailID = $('。gallery-thumbnail')。data('attachment-id');&#xA; var galleryThumbnailSrc = $('。gallery-缩略图')。attr('src');&#xA;&#xA; $(galleryThumbnail).each(function(){&#xA; var imgSrc = $(this).attr('src');& #xA; console.log(imgSrc);&#xA;});&#xA;&#xA; $(galleryThumbnail).each(function(){&#xA; var imgIDs = $(this).data( “attachment-id”);&#xA; console.log(imgIDs);&#xA;});&#xA;&#xA;}); &#xA;  
&#xA;&#xA;

在控制台中输出图像ID

&#xA;

2 个答案:

答案 0 :(得分:0)

您可以从按钮中选择父元素,然后从中查找要查找的内容。

类似这样的事情

var img = $(this).closest('li').find('.gallery-thumbnail');
var galleryThumbnail = img;
var galleryThumbnailID = img.data('attachment-id');         
var galleryThumbnailSrc = img.attr('src');

答案 1 :(得分:0)

首先,我认为添加像这样的事件处理程序会更加困惑:

$('.delete-custom-img').click(function() {...});

$('.delete-custom-img', imgContainer).click(function() {...});

如果在imgContainer之外还有其他元素,那么您不想将事件处理程序添加到。

但我想这是个人喜好,所以对你的问题: 问题是你得到了所有出现的&#39; .gallery-thumbnail&#39;在页面上,因为你没有指定jQuery的任何范围(如上面的imgContainer)。 因此,当您点击它时,您将处于删除按钮的范围内。在生成的标记中,它与缩略图共享同一个父级,因此您可以执行以下操作:

var galleryThumbnail = $('.gallery-thumbnail', $(this).parent());

第二个参数指定jQuery使用&#39; .gallery-thumbnail&#39;来搜索元素的范围。上课。

Haven没有测试过它,但我很确定这可以解决你的问题。

干杯, 沫