我使用jQuery sortable()连接了列表。
列表初始化为
$( "#held, #notheld" ).sortable({
connectWith: ".connectedSortable",
}).disableSelection();
$('#held li').on('dblclick', function() {
var litem = $(this).clone();
litem.appendTo($('#notheld'));
$(this).remove();
update_holding(litem.attr('id'), 'remove');
$( "#held, #notheld" ).sortable( "refresh" );
});
$('#notheld li').on('dblclick', function() {
var litem = $(this).clone();
litem.appendTo($('#held'));
$(this).remove();
update_holding(litem.attr('id'), 'add');
$( "#held, #notheld" ).sortable( "refresh" );
});
一旦克隆的LI被附加到另一个列表中,它就需要
对的
元素仍然可以被拖动到新列表中而不会出错。
我曾尝试在初始化调用中将绑定函数添加到activate、change和update事件中,希望refresh()能够看到新元素并执行.on()赋值,但这些都是无效的。
我还试着像这样重新编写初始绑定
$('#notheld li').on('dblclick', function() {
var litem = $(this).clone();
litem.appendTo($('#held'));
$(this).remove();
update_holding(litem.attr('id'), 'add');
litem.on('dblclick', function() {
var litem2 = $(this).clone();
litem2.appendTo($('#notheld'));
$(this).remove();
update_holding(litem2.attr('id'), 'remove');
});
});
但这不能正确调用函数吗?也许$(this)的用法不正确?
update_holding()函数不应该处理这个问题,因为它只是一个ajax帖子,指向另一个管理数据库更新的脚本。
https://jsfiddle.net/qn6v42c9/
jQuery clone() not cloning event bindings, even with on()
和
jquery .on() doesn't work for cloned element