我有两个无序的名单。动态填充一个无序列表,然后双击它的项,将其添加到另一个无序列表中。
我正在研究如何检测动态填充列表中的项是否还在另一个列表中。如果是的话。那就不应该再加了。不想添加重复的项目。
填充ul的代码:
.done(function(html) {
var results = jQuery.parseJSON(html);
$('#store_sel').children().remove();
for (var i = 0; i <= results.length; i++){
$("#store_selection").append("<li class='" + results[i].name + " stores'" + "id= " + results[i].uuid +
+ ">" + results[i].name + "</li>");
}
});活动:
$('#store_selection').on('dblclick', 'li', function(e) {
e.stopPropagation();
e.preventDefault();
if ($('#store_selected').find($(this))) {
console.log('item already there');
} else {
$('#store_selected').append($(this).clone());
//$(this).remove();
}
});编辑:为什么这不起作用?它基本上会转到console.log,即使ul是空的。
发布于 2014-11-03 11:34:04
if语句中有几个问题。
thislength因为ID在页面中必须是唯一的,所以我建议您使用data-属性来存储UUID
<li data-uuid="xxxx-yyy">然后当你搜索:
var uuid = $(this).data('uuid')
if ($('#store_selected').find('.stores[data-uuid='+uuid+']').length) {
console.log('item already there');
} else {
$('#store_selected').append($(this).clone());
//$(this).remove();
} https://stackoverflow.com/questions/26713110
复制相似问题