简体   繁体   English

jQuery的。 无法识别克隆的元素

[英]jQuery. Cloned element not recognized

I'm cloning some div by jQuery .clone(true) and inserting cloned element as last one, but then, when I'm trying to use selector :last for this element it's not working. 我正在通过jQuery .clone(true)克隆一些div,并将克隆的元素作为最后一个插入,但是然后,当我尝试对该元素使用选择器:last时,它不起作用。 Console do not show any error. 控制台不显示任何错误。 Here is how I clone element: 这是我克隆元素的方式:

            $("div.addnewitem").on('click', function () {
                var $d = $("#example").clone(true);
                $($d).removeAttr("style");
                $($d).insertAfter("div.item:last");
                $('html, body').animate({
                    scrollTop: $("div.item:last").offset().top
                }, 2000);
            });

This is element to clone: 这是要克隆的元素:

<div id="example" style="display: none;" class="item">
        <div class="deleteitem">
            <img alt="" src="../img/deleteitem.png">
        </div>
        <div class="itemphoto">
            <img alt="" src="../img/woolrich.png">
        </div>
        <div class="bigshadow">
            <img alt="" src="../img/bigshadow.png">
        </div>
        <div class="about">
            <input type="text" class="link" placeholder="Ссылка на изображение" name="item_image" />
            <input type="text" class="name" placeholder="Вещь и бренд..." name="item_name">
            <textarea class="review" placeholder="Короткое описание (около 120 символов)..." name="item_desc"></textarea>
            <input type="text" class="link" placeholder="Ссылка на вещь..." name="item_url">
            <input type="text" class="pricestylist" placeholder="Цена (xxx руб/$)" name="item_price">
        </div>
    </div>

An here is the way how I want to manipulate cloned element: 这是我要操纵克隆元素的方式:

$("input[name='item_url']:last").change(function (event) {
                if ($("#setname").val() != "") {
                    if ($("input[name='item_url']:last").val() != null) {
                        $.ajax({
                            url: '../ParseShop.asmx/ParseFott',
                            cache: false,
                            type: 'POST',
                            dataType: 'json',
                            contentType: 'application/json; charset=utf-8',
                            data: "{'urlstring':'" + $("input[name='item_url']:last").val() + "'}",
                            success: function (msg) {
                                $("textarea[name='item_desc']:last").val($.trim(msg.d.ItemDescription));
                                $("input[name='item_name']:last").val($.trim(msg.d.ItemName));
                                $("input[name='item_price']:last").val($.trim(msg.d.ItemPrice));
                                $("input[name='item_image']:last").val($.trim(msg.d.ItemImageUrl));
                                $("img[id='itemimg']:last").attr('src', $.trim(msg.d.ItemImageUrl));
                            },
                            error: function (xhr, status, error) {
                                var err = eval("(" + xhr.responseText + ")");
                                alert(err.Message);
                            }
                        });
                    }
                    else {
                        alert("Вы забыли ввести ссылку на товар!");
                        $("input[name='item_url']:last").focus();
                        event.stopPropagation();
                    }
                }
                else {
                    alert("Вы забыли название комплекта!");
                    $("#setname").focus();
                    event.stopPropagation();
                }
            });

PS Cloning function are working fine - I can clone and delete cloned elements, but why :last not working I do not have any idea. PS克隆功能可以正常工作-我可以克隆和删除克隆的元素,但是为什么:last不能正常工作我没有任何想法。

Well, here is the solution that working for me. 好吧,这是为我工作的解决方案。

$("div.item").on('change', 'input[name="item_url"]', function (event) {
                if ($("#setname").val() != "") {
                    if ($(this).val() != null) {
                        $.ajax({
                            url: '../ParseShop.asmx/ParseFott',
                            cache: false,
                            type: 'POST',
                            dataType: 'json',
                            contentType: 'application/json; charset=utf-8',
                            data: "{'urlstring':'" + $("input[name='item_url']:last").val() + "'}",
                            success: function (msg) {
                                $("div.item:last textarea[name='item_desc']").val($.trim(msg.d.ItemDescription));
                                $("div.item:last input[name='item_name']").val($.trim(msg.d.ItemName));
                                $("div.item:last input[name='item_price']").val($.trim(msg.d.ItemPrice));
                                $("div.item:last input[name='item_image']").val($.trim(msg.d.ItemImageUrl));
                                $("div.item:last img[id='itemimg']").attr('src', $.trim(msg.d.ItemImageUrl));
                            },
                            error: function (xhr, status, error) {
                                var err = eval("(" + xhr.responseText + ")");
                                alert(err.Message);
                            }
                        });
                    }
                    else {
                        alert("Вы забыли ввести ссылку на товар!");
                        $("input[name='item_url']:last").focus();
                        event.preventDefault();
                    }
                }
                else {
                    alert("Вы забыли название комплекта!");
                    $("#setname").focus();
                    event.preventDefault();
                }
            });

PS Based on Brian answer. PS基于布莱恩的答案。 Brian, you may add your comment as answer. Brian,您可以添加您的评论作为答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM