Jquery,无法删除克隆元素

时间:2013-06-27 11:41:17

标签: javascript jquery html jquery-ui

当我尝试在我的jquery代码中使用clone方法时,我遇到了一些问题。 我在这里列出了我想要的以及我面对的问题。

首先让我告诉我想要什么,我希望当我点击添加按钮时,我的代码应克隆给id的元素带有删除按钮的数据。 (的 JS Fiddle

这是我的代码:HTML

<table class="cloneelementtable">
    <tr>
        <td>
            <div class="priceandsize1row">&nbsp;</div>
        </td>
        <td>
            <div class="priceandsize2row">&nbsp;</div>
        </td>
        <td>
            <div class="priceandsize3row">&nbsp;</div>
        </td>
        <td>
            <div class="priceandsize4row">&nbsp;</div>
        </td>
        <td>
            <div class="priceandsize5row">&nbsp;</div>
        </td>
        <td>
            <div class="priceandsize6to9row">&nbsp;</div>
        </td>
        <td>
            <div class="priceandsize6to9row">&nbsp;</div>
        </td>
        <td>
            <div class="priceandsize6to9row">&nbsp;</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">&nbsp;</div>
        </td>
        <td>
            <div class="priceandsize2row">&nbsp;</div>
        </td>
        <td>
            <div class="priceandsize3row">&nbsp;</div>
        </td>
        <td>
            <div class="priceandsize4row">&nbsp;</div>
        </td>
        <td>
            <div class="priceandsize5row">&nbsp;</div>
        </td>
        <td>
            <div class="priceandsize6to9row">&nbsp;</div>
        </td>
        <td>
            <div class="priceandsize6to9row">&nbsp;</div>
        </td>
        <td>
            <div class="priceandsize6to9row">&nbsp;</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时克隆它,之后当我点击第一个减去图标时它只删除一次。之后,我无法删除更多。

有谁能告诉我该如何解决这类问题?

即。单击加号按钮进行克隆,然后单击删除按钮以删除克隆。

2 个答案:

答案 0 :(得分:3)

ID必须是唯一的。如果您有可能克隆节点,请确保将其ID属性替换为唯一的ID,并且永远不要在其中使用ID,除非您准备全部替换它们。喜欢上课。

答案 1 :(得分:3)

问题在于您的$("#toremoveclone").click(...)绑定。 你错过了一些东西。这是working fiddle

的问题:

  • ID应该是唯一的。
  • 您期待事件委派,而不进行设置。
  • $("#toremoveclone").click(...)仅将点击事件绑定到第一个#toremoveclone
  • 要检查添加超过2个克隆(您的代码需要多于1个克隆才能删除一个克隆)&amp;删除fiddle中的第一个。
  • 它会被删除。但其他人不会,因为他们没有受到任何点击事件的约束。

我所做的更改:

  • 为div添加了一个类(toremoveclone)。
  • 将事件绑定更改为

      

    $(“。cloneelementtable”)。on('click','。toremoveclone',function(){..})

  • here&amp ;;中了解有关Jquery事件授权的更多信息。 here,因为如果我解释一下,我可能搞砸了:(