doukeng7426 2015-02-16 19:13
浏览 476

如何从克隆中删除div(jQuery - 删除元素的父行)

I have add clone (more than 3) and remove clone button. when i try to clone

        <div class="data-add">
        <div class="form-group" id="id1">
            <label class="col-lg-2 control-label"> Image* : </label>
            <div class="col-lg-10">
<input id="input-702" name="userfile[]" type="file"class="file-loading">
        </div>
            </div>

    <div class="form-group clearfix" id="id2">
    <label class="col-lg-2 control-label">option* :
                </label>
    <div class="col-lg-10">
    <select name="option[]" autocomplete="off">
        <option>name1</option>
        <option>name2</option>
        <option>name3</option>
    </select>
    </div>
    </div>
</div>

  <button class="add"  type="button">Add Another </button>


   <button class="remove" id="remove">   remove  </button>  

$(".add").click(function(){
     $("#id1").clone().appendTo(".data-add");
     $("#id2").clone().appendTo(".data-add");
});

It clone particular div happening proper. when i click remove its not working (not able to remove)

$(".remove").click(function(){
    $(this).closest("#id1").remove();
         $(this).closest("#id2").remove();
});

i have tried this also

$(".remove").click(function(){
    $(this).parents("#id1").remove();
         $(this).parents("#id2").remove();
});

$(".remove").click(function(){
   $("#id1").remove();
   $("#id2").remove();
});

when i used like this it removing first clone(first id1 and id2). not removing current clone div

  • 写回答

2条回答 默认 最新

  • duandi4238 2015-02-16 19:23
    关注

    Change ID first of all, like below.

    var newId = 10; // Sample ID To Append
    var startID = ["id1_", id2_];
    $(".add").click(function(){
         var oldId = newId;
         startID[0].replace(oldId, newId);
         startID[1].replace(oldId, newId);
         var newId++;
         $("#id1").clone().attr('id', startID[0]).addClass("clonedDiv").appendTo(".data-add");
         $("#id2").clone().attr('id', startID[1]).addClass("clonedDiv").appendTo(".data-add");
    });
    

    Then use the new ID or the common class .clonedDiv to remove them.

    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀