Javascript 删除特定的附加元素

Javascript 删除特定的附加元素,javascript,jquery,Javascript,Jquery,假设我有复选框(a、b、c),如果选中了一个复选框(例如b),那么我将其副本附加到“x”类中,问题是“如何在初始复选框取消选中时删除(b)的创建副本?”。此时我正在删除所有创建的元素,因为我不知道其他解决方案。 $(“输入:复选框”)。打开(“更改”,函数(){ if($(this).prop('checked')==true){ $element=$(this.parent().clone().appendTo(“body”).addClass(“new”); $(“.new input”

假设我有复选框(a、b、c),如果选中了一个复选框(例如b),那么我将其副本附加到“x”类中,问题是“如何在初始复选框取消选中时删除(b)的创建副本?”。此时我正在删除所有创建的元素,因为我不知道其他解决方案。

$(“输入:复选框”)。打开(“更改”,函数(){
if($(this).prop('checked')==true){
$element=$(this.parent().clone().appendTo(“body”).addClass(“new”);
$(“.new input”).remove();
}
if($(this).prop('checked')==false){
$(“.new”).remove();
}
});

添加元素时,只需在新添加的元素上创建一个唯一的类,取消选中复选框时,使用新类删除元素

在这个答案中,我使用输入复选框名称创建了唯一的类。因此,新类是“new”+复选框\u name

$(“输入:复选框”)。打开(“更改”,函数(){
如果($(this.prop('checked')==true){
$element=$(this.parent().clone().appendTo(“body”).addClass(“new”+$(this.attr('name'));
$(“.new”+$(this.attr('name')+“input”).remove();
}
if($(this.prop('checked')==false){
$(“.new”+$(this.attr('name')).remove();
}
});

您可以根据
img
来源删除元素:

var-src;
$(“输入:复选框”)。打开(“更改”,函数(){
if($(this).prop('checked')==true){
$(this.parent().clone().appendTo(“body”).addClass(“new”);
$(“.new input”).remove();
}否则{
src=$(this.parent().find('img:first').prop('src');
$(“.new”).find(“[src=“”+src+“]”).remove();
}
});