选中复选框时,jQuery 复制其他 div 元素

发布于 2024-12-26 04:01:22 字数 1321 浏览 4 评论 0 原文

所以我有这个棘手的(至少对我来说)脚本需要编写/修改。您可以在下面看到它现在的样子:

HTML:

<div class="holder" style="float: left; margin-right: 20px">
    <input type=checkbox class="checkbox" checked="checked" />
    <h2>Lorem ipsum</h2>
</div>
<div class="holder" style="float: left; margin-right: 20px">
    <input type=checkbox class="checkbox" checked="checked" />
    <h2>Lorem ipsum</h2>
</div>
<div class="holder" style="float: left; margin-right: 20px">
    <input type=checkbox class="checkbox" checked="checked" />
    <h2>Lorem ipsum</h2>
</div>
<div class="holder" style="float: left; margin-right: 20px; clear: right">
    <input type=checkbox class="checkbox" checked="checked" />
    <h2>Lorem ipsum</h2>
</div>

<div class="number">
    0
</div>

Javascript:

var increment2=1; 
        $('.checkbox').live('click', function() { 
        $('.number').html( '(' + increment2 + ')'); 
        increment2++; 
    })

http://jsfiddle.net/P3zMu/13/

需要做的是,当您选中某个框时,容器的 h2 内容应在 ul 中显示为 li。如果选中了 3 个框,则 ul 中将是 3 li 等...当您取消选中该框时,它当然会从 ul 中删除。

我希望我的解释有意义。有人可以帮忙吗?

So I've got this tricky (at least for me) script to write/modify. You can see below how it looks right now:

HTML:

<div class="holder" style="float: left; margin-right: 20px">
    <input type=checkbox class="checkbox" checked="checked" />
    <h2>Lorem ipsum</h2>
</div>
<div class="holder" style="float: left; margin-right: 20px">
    <input type=checkbox class="checkbox" checked="checked" />
    <h2>Lorem ipsum</h2>
</div>
<div class="holder" style="float: left; margin-right: 20px">
    <input type=checkbox class="checkbox" checked="checked" />
    <h2>Lorem ipsum</h2>
</div>
<div class="holder" style="float: left; margin-right: 20px; clear: right">
    <input type=checkbox class="checkbox" checked="checked" />
    <h2>Lorem ipsum</h2>
</div>

<div class="number">
    0
</div>

Javascript:

var increment2=1; 
        $('.checkbox').live('click', function() { 
        $('.number').html( '(' + increment2 + ')'); 
        increment2++; 
    })

http://jsfiddle.net/P3zMu/13/

What needs to be done is that when you check on of the boxes the h2 content of the container should appear in the ul as li. If there are 3 boxes checked, than in the ul will be 3 li etc... when you uncheck the box it's ofcourse being removed from the ul.

I hope my explanation is making sens. Can anyone help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

用心笑 2025-01-02 04:01:22

我对您的 html 做了一些小的更改并实现了以下代码,

DEMO 这里

var increment2 = 1;

$('.checkbox').on('click', function() {
    $('.number').html('(' + increment2 + ')');
    increment2++;

    var $div = $(this).parent('div').get(0);

    if ($(this).is(':checked')) {        
        $('#selected_items').append('<li selected-item="' + $div.id + '_selected">' + $($div).find('h2').text() + '</li>');
    } else {
        $('#selected_items > li[selected-item^="' + $div.id + '"]').remove();
    }
});

I made few minor changes to your html and implemented below code,

DEMO here

var increment2 = 1;

$('.checkbox').on('click', function() {
    $('.number').html('(' + increment2 + ')');
    increment2++;

    var $div = $(this).parent('div').get(0);

    if ($(this).is(':checked')) {        
        $('#selected_items').append('<li selected-item="' + $div.id + '_selected">' + $($div).find('h2').text() + '</li>');
    } else {
        $('#selected_items > li[selected-item^="' + $div.id + '"]').remove();
    }
});
北渚 2025-01-02 04:01:22

使用clone()方法的东西应该可以工作:

var increment2=1; 
    $('.checkbox').live('click', function() {  
    increment2++; 
    $('.number').append($(this).next().clone());
})

这只会将复选框后的下一个元素(在本例中为h2)附加到.number元素,但你明白了。为了使其正常工作,您需要为每个值输入不同的值而不是 lorem ipsum,然后检查它们是否已经存在/在取消选中时删除它们 - 没有时间写下来。

Something using the clone() method should work:

var increment2=1; 
    $('.checkbox').live('click', function() {  
    increment2++; 
    $('.number').append($(this).next().clone());
})

That will just append the next element after the checkbox (the h2 in this case) to the .number element, but you get the idea. In order for this to work correctly, you would need to put in different values instead of lorem ipsum for each, then check to see if they already exist/remove them on uncheck - didn't have time to write that up.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文