0

当我试图在选中复选框的两个列表之间移动 li 时,反之亦然,一些如何设法从选中的第一个列表移动到第二个列表,但如果我取消选中复选框,则坚持将 li 移回

这是小提琴http://jsfiddle.net/B4XhH/6/

$("input[type=checkbox]").click(function () {    
var i=$('input:checkbox').index($(this));
if(confirm("Are you sure to move ? ")){
    $( '#wizard1 li:eq('+i+')' ).insertAfter( '#wizard2 li:last' );
}else{
    if($(this).is(":checked")){
         alert("gdfg");
         $(':checkbox').each(function(){ this.checked = false; });
         //$("input[type=checkbox]").prop('checked', false);
     }
}

});

4

1 回答 1

0

像这样的东西?

$("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'));
        }
    }
});

小提琴

于 2013-10-14T02:59:22.313 回答