当我尝试在我的jquery代码中使用clone方法时,我遇到了一些问题。 我在这里列出了我想要的以及我面对的问题。
首先让我告诉我想要什么,我希望当我点击添加按钮时,我的代码应克隆给id的元素带有删除按钮的数据。 (的 JS Fiddle )
这是我的代码:HTML
<table class="cloneelementtable">
<tr>
<td>
<div class="priceandsize1row"> </div>
</td>
<td>
<div class="priceandsize2row"> </div>
</td>
<td>
<div class="priceandsize3row"> </div>
</td>
<td>
<div class="priceandsize4row"> </div>
</td>
<td>
<div class="priceandsize5row"> </div>
</td>
<td>
<div class="priceandsize6to9row"> </div>
</td>
<td>
<div class="priceandsize6to9row"> </div>
</td>
<td>
<div class="priceandsize6to9row"> </div>
</td>
<td id="divaddmore">
<div class="priceandsize6to9row" id="todoclone">
<img src="http://cdn2.iconfinder.com/data/icons/icojoy/noshadow/standart/gif/24x24/001_01.gif" width="30" height="30">
</div>
</td>
<td>
<div class="priceandsize10row">
<div class="tablecolorgrey">B</div>
</div>
</td>
<td>
<div class="priceandsizeinputsrow">
<div class="aviailableinput">
<input name="" type="text" placeholder="15">
</div>
</div>
</td>
<td>
<div class="priceandsizeinputsrow">
<div class="aviailableinput">
<input name="" type="text" placeholder="15">
</div>
</div>
</td>
<td>
<div class="priceandsizeinputsrow">
<div class="aviailableinput">
<input name="" type="text" placeholder="15">
</div>
</div>
</td>
<td>
<div class="priceandsizeinputsrow">
<div class="aviailableinput">
<input name="" type="text" placeholder="15">
</div>
</div>
</td>
<td>
<div class="priceandsizeinputsrow">
<div class="aviailableinput">
<input name="" type="text" placeholder="15">
</div>
</div>
</td>
<td>
<div class="priceandsizeinputsrow">
<div class="aviailableinput">
<input name="" type="text" placeholder="15">
</div>
</div>
</td>
<div id="showsizeheader">
<td id="tdhide1" style="display:none;">
<div class="priceandsizeinputsrow">
<div class="aviailableinput">
<input name="" type="text" placeholder="16">
</div>
</div>
</td>
<td id="tdhide2" style="display:none;">
<div class="priceandsizeinputsrow">
<div class="aviailableinput">
<input name="" type="text" placeholder="17">
</div>
</div>
</td>
<td id="tdhide3" style="display:none;">
<div class="priceandsizeinputsrow">
<div class="aviailableinput">
<input name="" type="text" placeholder="18">
</div>
</div>
</td>
<td id="tdhide4" style="display:none;">
<div class="priceandsizeinputsrow">
<div class="aviailableinput">
<input name="" type="text" placeholder="19">
</div>
</div>
</td>
<td id="tdhide5" style="display:none;">
<div class="priceandsizeinputsrow">
<div class="aviailableinput">
<input name="" type="text" placeholder="20">
</div>
</div>
</td>
<td id="tdhide6" style="display:none;">
<div class="priceandsizeinputsrow">
<div class="aviailableinput">
<input name="" type="text" placeholder="21">
</div>
</div>
</td>
<td id="tdhide7" style="display:none;">
<div class="priceandsizeinputsrow">
<div class="aviailableinput">
<input name="" type="text" placeholder="22">
</div>
</div>
</td>
<td id="tdhide8" style="display:none;">
<div class="priceandsizeinputsrow">
<div class="aviailableinput">
<input name="" type="text" placeholder="23">
</div>
</div>
</td>
<td id="tdhide9" style="display:none;">
<div class="priceandsizeinputsrow">
<div class="aviailableinput">
<input name="" type="text" placeholder="24">
</div>
</div>
</td>
<td id="tdhide10" style="display:none;">
<div class="priceandsizeinputsrow">
<div class="aviailableinput">
<input name="" type="text" placeholder="25">
</div>
</div>
</td>
<td id="tdhide11" style="display:none;">
<div class="priceandsizeinputsrow">
<div class="aviailableinput">
<input name="" type="text" placeholder="26">
</div>
</div>
</td>
<td id="tdhide12" style="display:none;">
<div class="priceandsizeinputsrow">
<div class="aviailableinput">
<input name="" type="text" placeholder="27">
</div>
</div>
</td>
<td id="tdhide13" style="display:none;">
<div class="priceandsizeinputsrow">
<div class="aviailableinput">
<input name="" type="text" placeholder="28">
</div>
</div>
</td>
<td id="tdhide14" style="display:none;">
<div class="priceandsizeinputsrow">
<div class="aviailableinput">
<input name="" type="text" placeholder="29">
</div>
</div>
</td>
<td id="tdhide15" style="display:none;">
<div class="priceandsizeinputsrow">
<div class="aviailableinput">
<input name="" type="text" placeholder="30">
</div>
</div>
</td>
<td id="tdhide16" style="display:none;">
<div class="priceandsizeinputsrow">
<div class="aviailableinput">
<input name="" type="text" placeholder="31">
</div>
</div>
</td>
<td id="tdhide17" style="display:none;">
<div class="priceandsizeinputsrow">
<div class="aviailableinput">
<input name="" type="text" placeholder="32">
</div>
</div>
</td>
</div>
<td>
<div class="priceandsize10row">
<div class="tablecolorgrey">140</div>
</div>
</td>
<td>
<div class="priceandsize11row">
<div class="tablecolorgrey">$4815.00</div>
</td>
<td>
<div class="priceandsize11row" id="showsize">
<img id="showimg" src="images/img_click_moresizes.jpg" width="35" height="30">
</div>
</td>
</tr>
<tr id="cloneelement">
<td>
<div class="priceandsize1row"> </div>
</td>
<td>
<div class="priceandsize2row"> </div>
</td>
<td>
<div class="priceandsize3row"> </div>
</td>
<td>
<div class="priceandsize4row"> </div>
</td>
<td>
<div class="priceandsize5row"> </div>
</td>
<td>
<div class="priceandsize6to9row"> </div>
</td>
<td>
<div class="priceandsize6to9row"> </div>
</td>
<td>
<div class="priceandsize6to9row"> </div>
</td>
<td>
<div class="priceandsize6to9row" id="toremoveclone">
<img src="http://cdn2.iconfinder.com/data/icons/32pxmania/buttons_21.png" width="30" height="30">
</div>
</td>
<td>
<div class="priceandsize10row">
<div class="tablecolorgrey">B</div>
</div>
</td>
<td>
<div class="priceandsizeinputsrow">
<div class="aviailableinput">
<input name="" type="text" placeholder="15">
</div>
</div>
</td>
<td>
<div class="priceandsizeinputsrow">
<div class="aviailableinput">
<input name="" type="text" placeholder="15">
</div>
</div>
</td>
<td>
<div class="priceandsizeinputsrow">
<div class="aviailableinput">
<input name="" type="text" placeholder="15">
</div>
</div>
</td>
<td>
<div class="priceandsizeinputsrow">
<div class="aviailableinput">
<input name="" type="text" placeholder="15">
</div>
</div>
</td>
<td>
<div class="priceandsizeinputsrow">
<div class="aviailableinput">
<input name="" type="text" placeholder="15">
</div>
</div>
</td>
<td>
<div class="priceandsizeinputsrow">
<div class="aviailableinput">
<input name="" type="text" placeholder="15">
</div>
</div>
</td>
<div id="showsizeheader">
<td id="tdhide1" style="display:none;">
<div class="priceandsizeinputsrow">
<div class="aviailableinput">
<input name="" type="text" placeholder="16">
</div>
</div>
</td>
<td id="tdhide2" style="display:none;">
<div class="priceandsizeinputsrow">
<div class="aviailableinput">
<input name="" type="text" placeholder="17">
</div>
</div>
</td>
<td id="tdhide3" style="display:none;">
<div class="priceandsizeinputsrow">
<div class="aviailableinput">
<input name="" type="text" placeholder="18">
</div>
</div>
</td>
<td id="tdhide4" style="display:none;">
<div class="priceandsizeinputsrow">
<div class="aviailableinput">
<input name="" type="text" placeholder="19">
</div>
</div>
</td>
<td id="tdhide5" style="display:none;">
<div class="priceandsizeinputsrow">
<div class="aviailableinput">
<input name="" type="text" placeholder="20">
</div>
</div>
</td>
<td id="tdhide6" style="display:none;">
<div class="priceandsizeinputsrow">
<div class="aviailableinput">
<input name="" type="text" placeholder="21">
</div>
</div>
</td>
<td id="tdhide7" style="display:none;">
<div class="priceandsizeinputsrow">
<div class="aviailableinput">
<input name="" type="text" placeholder="22">
</div>
</div>
</td>
<td id="tdhide8" style="display:none;">
<div class="priceandsizeinputsrow">
<div class="aviailableinput">
<input name="" type="text" placeholder="23">
</div>
</div>
</td>
<td id="tdhide9" style="display:none;">
<div class="priceandsizeinputsrow">
<div class="aviailableinput">
<input name="" type="text" placeholder="24">
</div>
</div>
</td>
<td id="tdhide10" style="display:none;">
<div class="priceandsizeinputsrow">
<div class="aviailableinput">
<input name="" type="text" placeholder="25">
</div>
</div>
</td>
<td id="tdhide11" style="display:none;">
<div class="priceandsizeinputsrow">
<div class="aviailableinput">
<input name="" type="text" placeholder="26">
</div>
</div>
</td>
<td id="tdhide12" style="display:none;">
<div class="priceandsizeinputsrow">
<div class="aviailableinput">
<input name="" type="text" placeholder="27">
</div>
</div>
</td>
<td id="tdhide13" style="display:none;">
<div class="priceandsizeinputsrow">
<div class="aviailableinput">
<input name="" type="text" placeholder="28">
</div>
</div>
</td>
<td id="tdhide14" style="display:none;">
<div class="priceandsizeinputsrow">
<div class="aviailableinput">
<input name="" type="text" placeholder="29">
</div>
</div>
</td>
<td id="tdhide15" style="display:none;">
<div class="priceandsizeinputsrow">
<div class="aviailableinput">
<input name="" type="text" placeholder="30">
</div>
</div>
</td>
<td id="tdhide16" style="display:none;">
<div class="priceandsizeinputsrow">
<div class="aviailableinput">
<input name="" type="text" placeholder="31">
</div>
</div>
</td>
<td id="tdhide17" style="display:none;">
<div class="priceandsizeinputsrow">
<div class="aviailableinput">
<input name="" type="text" placeholder="32">
</div>
</div>
</td>
</div>
<td>
<div class="priceandsize10row">
<div class="tablecolorgrey">140</div>
</div>
</td>
<td>
<div class="priceandsize11row">
<div class="tablecolorgrey">$4815.00</div>
</td>
</tr>
</table>
代码:JQ
var id = 0;
$(document).ready(function () {
$('#todoclone').click(function () {
$('#cloneelement').clone().appendTo(".cloneelementtable");
id++;
});
/*$('#toremoveclone').click(function() {
$('#cloneelement').closest('#cloneelement').fadeOut("slow", function(){
$(this).remove();
});
});*/
$("#toremoveclone").click(function () {
console.log(id);
if (id > 1) { //Only apply if the lang field is more than 1
id = id - 1;
$("#cloneelement:last").remove();
console.log(id);
}
});
});
我对此进行了一些搜索并制作了相似的代码(代码在这里:JS Fiddle )
但代码中存在一些问题,(如下所列) 1]我无法删除克隆元素,我需要在atlist 3时克隆它,之后当我点击第一个减去图标时它只删除一次。之后,我无法删除更多。
有谁能告诉我该如何解决这类问题?
即。单击加号按钮进行克隆,然后单击删除按钮以删除克隆。
答案 0 :(得分:3)
ID必须是唯一的。如果您有可能克隆节点,请确保将其ID属性替换为唯一的ID,并且永远不要在其中使用ID,除非您准备全部替换它们。喜欢上课。
答案 1 :(得分:3)
问题在于您的$("#toremoveclone").click(...)
绑定。
你错过了一些东西。这是working fiddle。
$("#toremoveclone").click(...)
仅将点击事件绑定到第一个#toremoveclone
。