【问题标题】:How can I remove clone HTML JQuery如何删除克隆 HTML JQuery
【发布时间】:2015-12-31 17:28:12
【问题描述】:

我只是一个初学者,我想为删除克隆创建按钮,但我做不到。 这是我的代码,但它不起作用。请帮助我知道我哪里做错了。

PS。对不起我的英语不好

HTML

 <div class="docu">
      <div class="row">
         <div class="col-sm-2"></div>
            <div class="col-lg-7"  id="Freport" name="Freport">
              <div class="input-group">
                <div class="input-group-btn">
                    <select name="tell" id="tell" class="btn btn-default dropdown-toggle">
                        <option value="0"></option>
                        <option value="1"></option>
                        <option value="2"></option>
                        <option value="3"></option>
                    </select>
      </div><!--End row-->

      <input type="text" class="form-control" aria-label="..." placeholder="...">

      </div><!-- /btn-group -->
    </div><!-- /input-group -->

         <div class="col-sm-1">

<button type="submit" id="btnClonereport"><span class="glyphicon glyphicon-plus-sign" aria-hidden="true"></span></button>
<button type="submit" id="btnDelreport"><span class="glyphicon glyphicon-minus-sign" aria-hidden="true"></span></button>
</div>

<div id="container4">
</div>

JS这是我的脚本,我可以克隆但我无法删除克隆。

<script type="text/javascript">
    $(function () {
        $("#btnClonereport").bind("click", function () {


            var index = $("#container4 select").length + 1;

            //Clone the DropDownList
            var fre = $("#Freport").clone();
            var fre2 = $("#orand").clone();


            //Set the ID and Name
            fre.attr("id", "Freport_" + index);
            fre.attr("name", "Freport_" + index);
            fre2.attr("id", "orand_" + index);
            fre2.attr("name", "orand_" + index);


            //[OPTIONAL] Copy the selected value
            var selectedValue = $("#Freports option:selected").val();
            fre.find("option[value = '" + selectedValue + "']").attr("selected", "selected");
            var selectedValue = $("#orands option:selected").val();
            fre2.find("option[value = '" + selectedValue + "']").attr("selected", "selected");

            //Append to the DIV.


            $("#container4").append(fre2);
            $("#container4").append(fre);
            $("#container4").append("<br /><br />");


        });
        $("#btnDelreport").bind("click", function(){
            $(this).closest("#container4").remove();
    });
    });

</script>

【问题讨论】:

  • 你不认为你应该为每个克隆元素设置删除按钮吗?

标签: javascript jquery html clone


【解决方案1】:

所以你想删除所有的表单克隆?试试这个删除按钮操作:

编辑:如果您想删除最后一次出现,这会使事情变得更简单。

    $("#btnDelreport").bind("click", function(){
        $('#container4').children('.col-lg-7').last().remove();
    });

【讨论】:

  • 谢谢它的工作,但我想删除最新的克隆。你能帮帮我吗?
  • 非常感谢您的帮助^^
猜你喜欢
  • 1970-01-01
  • 2012-12-02
  • 2012-05-07
  • 1970-01-01
  • 1970-01-01
  • 2012-02-13
  • 2018-03-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多