【问题标题】:Calculate the children in the parent div and removing each child by clicking on the button untill last child计算父 div 中的子项并通过单击按钮删除每个子项,直到最后一个子项
【发布时间】:2018-01-29 10:40:02
【问题描述】:

我正在努力删除子 div,除了每个父 div 中的最后一个具有相同的类,我的代码结构如下,我的 Java 脚本删除子 div 名称重复-div:

$('.repeatable').on('click', '.close-repeating-div', function(e) {
  e.preventDefault();
  var countdiv = $('.repeatable').children('.repeating-div').length;
  if (countdiv > 1) {
    $(this).closest('.repeating-div').remove();
  }
});
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/js/bootstrap.min.js" integrity="sha384-a5N7Y/aK3qNeh15eJKGWxsqtnX/wWdSZSKp+81YjTmS15nvnvxKHuzaWwXHDli+4" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" integrity="sha384-Zug+QiDoJOrZ5t4lssLdxGhVrurbmBWopoEl+M6BdEfwnCJZtKxi1KgxUyJq13dy" crossorigin="anonymous">
<div class="repeatable">
  <div class="repeating-div">
    <input type="text" class="form-control">
    <a href="#" class="btn close-repeating-div form-control"><i class="fa fa-remove" aria-hidden="true"></i></a>
  </div>
  <div class="repeating-div">
    <input type="text" class="form-control">
    <a href="#" class="btn close-repeating-div form-control"><i class="fa fa-remove" aria-hidden="true"></i></a>
  </div>
  <div class="repeating-div">
    <input type="text" class="form-control">
    <a href="#" class="btn close-repeating-div form-control"><i class="fa fa-remove" aria-hidden="true"></i></a>
  </div>
</div>
<div class="repeatable">
  <div class="repeating-div">
    <input type="text" class="form-control">
    <a href="#" class="btn close-repeating-div form-control"><i class="fa fa-remove" aria-hidden="true"></i></a>
  </div>
  <div class="repeating-div">
    <input type="text" class="form-control">
    <a href="#" class="btn close-repeating-div form-control"><i class="fa fa-remove" aria-hidden="true"></i></a>
  </div>
  <div class="repeating-div">
    <input type="text" class="form-control">
    <a href="#" class="btn close-repeating-div form-control"><i class="fa fa-remove" aria-hidden="true"></i></a>
  </div>
</div>

在这里,如果每个父级中分别有多个子 div,我想删除子 div,但我的代码正在计算一个流中的所有子 div,但我需要单独计算它们,如果有多个,则需要删除它们每个父 div 中的子 div

【问题讨论】:

    标签: javascript jquery html bootstrap-4


    【解决方案1】:

    你需要指定你只在当前.repeatable中搜索:

     var countdiv = $(this).closest('.repeatable').children('.repeating-div').length;
    

    上面的代码意味着你从触发你的事件的元素(.close-repeating-div)开始,到它的.repeatable父元素,然后只计算这个div的子元素。

    【讨论】:

      【解决方案2】:

      下面的 javascript 使用了正确的范围:

      $('.repeatable').on('click', '.close-repeating-div' ,function(e){
          e.preventDefault();
          
          // `this` is the clicked <a> tag
          var parent = $(this).closest('.repeatable'),
              countdiv = parent.children('.repeating-div').length;
      
          if( countdiv > 1 ){
             $(this).closest('.repeating-div').remove();
          }
      });

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-11-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-08
        • 1970-01-01
        • 2015-11-07
        • 1970-01-01
        相关资源
        最近更新 更多