jQuery从变量中删除输入

时间:2019-05-08 14:06:19

标签: jquery

我有特定的div,我想用jQuery复制。复制后,我想对其进行一些修改,并删除ID为CustomerDeliveryaddressID的Input。最后,我想将其附加到某处。不幸的是,删除该部分对我不起作用。我怎样才能做到这一点?这是我的代码。

  $("#addDeliveryAddress").click(function(){
    var content = $('.deliveryAddressBox').html();
    content = "<div class='deliveryAddressBox'>"+content+"</div>";
    content = $(content).remove("#CustomerDeliveryaddressId");
    $(".deliveryAddressesContainer").append(content);
  });

3 个答案:

答案 0 :(得分:1)

与其删除并保存到内容中,不如尝试将您的删除行替换为

$("#addDeliveryAddress").click(function(){
    var $content = $('.deliveryAddressBox:last').clone();
    $content.wrapAll( "<div class='deliveryAddressBox'/>");
    $content.find("#CustomerDeliveryaddressId").remove();
    $(".deliveryAddressesContainer").append($content);
});

答案 1 :(得分:0)

确保不要将remove()的响应存储在会覆盖所需值的变量中。

$("#addDeliveryAddress").click(function(){
    var $content = $( $('.deliveryAddressBox').html() );

    // wrap the contents with the div
    $content.wrapAll("<div class='deliveryAddressBox'>");
    // remove the element you do not want
    $content.find("#CustomerDeliveryaddressId").remove();
    // append it to the DOM
    $(".deliveryAddressesContainer").append($content);
});

答案 2 :(得分:0)

$("#addDeliveryAddress").click(function(){
    var content = $('.deliveryAddressBox').html();
    content = "<div class='deliveryAddressBox'>"+content+"</div>";
    content = $(content).remove("#CustomerDeliveryaddressId");
    $(".deliveryAddressesContainer").append(content);
  });
  
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

嗨,用$(content).remove("#CustomerDeliveryaddressId");代替$(content).remove("#CustomerDeliveryaddressId");