选中复选框时,jQuery 复制其他 div 元素
所以我有这个棘手的(至少对我来说)脚本需要编写/修改。您可以在下面看到它现在的样子:
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++;
})
需要做的是,当您选中某个框时,容器的 h2 内容应在 ul 中显示为 li。如果选中了 3 个框,则 ul 中将是 3 li 等...当您取消选中该框时,它当然会从 ul 中删除。
我希望我的解释有意义。有人可以帮忙吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我对您的 html 做了一些小的更改并实现了以下代码,
DEMO 这里
I made few minor changes to your html and implemented below code,
DEMO here
使用clone()方法的东西应该可以工作:
这只会将复选框后的下一个元素(在本例中为h2)附加到.number元素,但你明白了。为了使其正常工作,您需要为每个值输入不同的值而不是 lorem ipsum,然后检查它们是否已经存在/在取消选中时删除它们 - 没有时间写下来。
Something using the clone() method should work:
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.