开发者

How to remove the checked checkboxes from the list and add them to another list

How do I remove the checked checkboxes from a list and add them to another list?

      $('#mer').click( function() {
         //var unchecked = [];
       $('input[type="checkbox"]').each (function() {
       if (this.checked) {
         var txt = $(开发者_如何转开发this).next("label").text();
         $('#ch').append($('<li>').append($(this).clone()).append(txt));
         $(':checkbox:checked').attr('disabled', true);  
         $(':checkbox:checked').remove();
          //$('.checklist').hide();
         $('#ch').show();
         //$('input[type="checkbox"]:not(:checked)').show('.checklist');
       } /*else {
                unchecked.push(this);   
                var txt1 = $(this).next("label").text(); 
                $('.checklist').append($('<li/>').append($(this).clone()).append(txt1));
                 $('.checklist').show();

            }*/
       });


It is very simple:

$('#from :checked').appendTo('#to');

Where #from is the parent (aka. select/checkbox parent) and to is #to is other select or parent you wish to put it to.


I'm not sure exactly what you are trying to do, but something like this might work:

$('input[type="checkbox"]').each (function() {
if($(this).is('input:checkbox:checked')){
    $(this).appendTo('whereverClassOrElement').remove();
}
});


I'm not sure exactly what you need, but here's a neat swapping sample. If you set up your html like this:

<h3>List One</h3>
<ul id="listOne">
    <li><label for="a">A</label><input type="checkbox" id="a" name="listOne"/></li>
    <li><label for="b">B</label><input type="checkbox" id="b" name="listOne"/></li>
    <li><label for="c">C</label><input type="checkbox" id="c" name="listOne"/></li>
</ul>

<br/>
<h3>List Two</h3>
<ul id="listTwo">
    <li><label for="d">D</label><input type="checkbox" id="d" name="listTwo" checked/></li>
</ul>

Then you can write relatively simple jQuery like this:

$(document).ready(function(){
    $('input[type=checkbox]').live('click', function(event){
        var t = $(this);
        var from = 'listOne';
        var to = 'listTwo';
        if (!t.is(':checked')){
            var swap = to;
            to = from;
            from = swap;
        }
        $('#'+to).append(t.attr('name', to).parent());
    });
});

I have this working in a jsfiddle: http://jsfiddle.net/FgWhr/5/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜