Jquery 选中复选框可将li从一个列表移动到另一个列表,取消选中可恢复到原始位置
当我试图在两个列表之间移动li时,请选中复选框,反之亦然 有些人试图在选中时从第一个列表移动到第二个列表,但如果我取消选中复选框,就必须向后移动li 这是小提琴 })) 像这样的Jquery 选中复选框可将li从一个列表移动到另一个列表,取消选中可恢复到原始位置,jquery,Jquery,当我试图在两个列表之间移动li时,请选中复选框,反之亦然 有些人试图在选中时从第一个列表移动到第二个列表,但如果我取消选中复选框,就必须向后移动li 这是小提琴 })) 像这样的 $("input[type=checkbox]").change(function () { var $this = $(this), i = $this.closest('li').index(), //get the index of its parent li
$("input[type=checkbox]").change(function () {
var
$this = $(this),
i = $this.closest('li').index(), //get the index of its parent li
$target = $('#wizard2'); //default set the target as wizard2
if (confirm("Are you sure to move ? ")) {
if (!this.checked) { //if unchecked
$target = $('#wizard1'); //set the target as wizard1
} else {
$this.data('idx', i); //set the index in data api
}
var $targetLi = $target.find('li:eq(' + $this.data('idx') + ')'); //get the target li to see if this has the item li with index
if ($targetLi.length) { //if yes the append this item before that
$targetLi.before($this.closest('li'));
} else { //else append it to the ul
$target.append($this.closest('li'));
}
}
});
将项目移回左侧时,它是应该位于列表底部还是回到原始位置?顺便说一句,ID必须是唯一的。项目应该去原来的位置感谢吨正是我want@user2234768-您说过,如果您取消选中右侧的项目,则该项目应显示在其原始位置。这样做不起作用。@j08691“是”重复移位时,部分项目被移到新位置location@user2234768啊,我明白了,检查一下这把小提琴,我肯定它不完美。
$("input[type=checkbox]").change(function () {
var
$this = $(this),
i = $this.closest('li').index(), //get the index of its parent li
$target = $('#wizard2'); //default set the target as wizard2
if (confirm("Are you sure to move ? ")) {
if (!this.checked) { //if unchecked
$target = $('#wizard1'); //set the target as wizard1
} else {
$this.data('idx', i); //set the index in data api
}
var $targetLi = $target.find('li:eq(' + $this.data('idx') + ')'); //get the target li to see if this has the item li with index
if ($targetLi.length) { //if yes the append this item before that
$targetLi.before($this.closest('li'));
} else { //else append it to the ul
$target.append($this.closest('li'));
}
}
});