【问题标题】:find count of rows added for clicked element查找为单击的元素添加的行数
【发布时间】:2017-07-19 20:19:06
【问题描述】:

当我想要“添加行”选项时,我有一个表,但我希望添加的行是同一行,即使用行跨度。现在我希望根据我点击添加行按钮的次数来增加行数。

HTML:

 <table class="table table-bordered datatabl dt-responsive">

      <thead>
      <tr>
      <th>&nbsp;</th>
      <!-- <th>Billing Status</th> -->
      <th>Skills</th>
      <th>Experience (in Years)</th>
      <th>Head Count</th>
      <!-- <th>Billing State</th> -->
      <th>Action</th>
      </tr>

      </thead>
      <tbody>


      <tr>
      <td class=""><a href="javascript:void(0);">Feb</a>
      <div class="popverdiv col-lg-4 col-md-4 col-xs-12 col-sm-12">
      <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
      <label>From</label>
        <select class="form-control">
            <option>1</option>
            <option>2</option>
            <option>3</option>
            <option>4</option>
        </select>

      </div>

      <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
      <label>To</label>
        <select class="form-control">
            <option>1</option>
            <option>2</option>
            <option>3</option>
            <option>4</option>
        </select>

      </div>

      <i class="glyphicon glyphicon-play"></i>

      <div class="popverdiv-buttons">
      <a class="btn btn-primary setastraveldates">Set Date</a>
      <a class="btn btn-default">Cancel</a>
      </div>
      </div>
      </td>

      <td><select class="form-control skil"><option>Select</option><option>HTML</option><option>JAVA</option></select></td>
      <td><select class="form-control exp"><option>Select</option><option>1-3</option><option>3-5</option></select></td>
      <td><input type="text" class="form-control headcount"/></td>

      <td>
   <select class="form-control">
      <option>Select</option>
      <option>Copy to Next</option>
      <option>Copy to all</option>
    </select>

    <a class="addrow">+</a>



     </td>

      </tr>




      </tbody>
      </table>

JS:

 $(".addrow").on("click",function(){
    $(this).parents("tr").after('<tr class="rowadded" role="row" class="odd"><td tabindex="0"><span class="">Jan</span></td><td><select class="form-control skil"><option>Select</option><option>HTML</option><option>JAVA</option></select></td><td><select class="form-control exp"><option>Select</option><option>1-3</option><option>3-5</option></select></td><td class="sorting_1"><input type="text" class="form-control headcount"></td><td><select class="form-control"><option>Select</option><option>Copy to Next</option><option>Copy to all</option></select></td></tr>');


    var i =0;
    $(this).parents("tr").next(".rowadded").each(function(){
    i++;

    });
    console.log(i);
    });

但我的代码不计算编号。 'rowadded' 类被添加到 addrow 按钮所在的行旁边,并重置下一个 addrow 按钮的计数。请帮忙。

【问题讨论】:

标签: jquery


【解决方案1】:

试试这个fiddle

var i =0;
$(".rowadded").each(function(){
i++;

});
console.log(i);

检查控制台将显示实际添加的行数。

【讨论】:

  • Arunkumar G,你的小提琴有问题。如果您已经添加了 2 行,然后单击第一行的添加按钮 2 次,然后单击第二行的添加行按钮,计数将是 3,而应该是 1。这就是为什么我说问题中的“重置”。
  • 代码中的添加按钮是静态的,不会动态添加到创建的新行中。因此,在任何时候,如果我们点击添加行按钮,我们将只获得点击总数,而不是特定行的点击。也许我没有得到你的要求。请详细说明。
【解决方案2】:

只需使用length 而不是each,如下所示:

$(this).parents("tr").next(".rowadded").length;  //returns 1
$('.rowadded').length; //returns total count

【讨论】:

  • 如果对您有帮助,请接受答案或点赞? @user3450590
  • 对不起,这似乎是错误的,因为 $('.rowadded').length 返回 .rowadded 在所有表中的长度,这是错误的
  • 你正在尝试的也是一样的......使用每个你都在增加变量@user3450590
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-06-28
  • 1970-01-01
  • 1970-01-01
  • 2019-05-19
  • 2019-07-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多