我在一个div中上传的图片很少,我想将它们移动到另一个div并更新数据库表。为此我需要所选图像的id和我要移动的div的名称。
我使用复选框获取所选图像的ID但我不知道如何才能获得所有ID。
function MoveImages(){
for ( var i = 0; i < $(".ct input[type=checkbox]:checked").length; i++) {
var element = $(".ct input[type=checkbox]:checked")[i];
var parent = element.parentNode;
var id = parent.getAttribute('id');
}
}
我怎样才能最终得到所有的id? 这就是我班级的样子。
<div class="ct" id="559429bc0d559162552c9728">
<input type="checkbox" class="remove">
<img src="/image?id=c9728" data-src="random.JPG" id="previewImagec9728">
</div>
move函数应返回所有id。
答案 0 :(得分:1)
使用一些JQuery的$ .each,substring和JS数组方法,您可以获取原始ID并将它们放入一个数组中,如下所示:
var ids = [];
//For each element that matches ".ct input[type=checkbox]:checked".
$.each($('.ct input[type=checkbox]:checked'), function() {
//Find the image element, get the id value, and strip the first 12 characters.
var id = $(this).find('img').attr('id').substring(12);
//Put ID in array.
ids.push(id);
});
答案 1 :(得分:1)
使用$.map()
:
var yourIds = $(".ct input[type=checkbox]:checked").map(function(){
return $(this).parent().attr('id');
});
答案 2 :(得分:0)
尝试各种jquery:
Removing branch branch1
Deleted branch branch1 (was ab1575a).
Removing branch branch2
Deleted branch branch2 (was c9a24c2).
答案 3 :(得分:0)
使用:has
和.map
$('.ct:has(:checked)').toArray().map(function(element, index) { return element.id })
或
$('.ct:has(:checked)').map(function(index, element) { return element.id }).toArray()
在两种情况下.toArray()
都是获取普通数组而不是jquery数组