javascript - 如何使用 jquery clone 增加每次点击的 TR?
问题描述
我有一个单行表,我想在每次单击时增加一行,但是当我第一次单击时,它工作正常,但是当我第二次单击时,它增加了两次。
我还想增加每个增量的计数。
我在下面给出的尝试: -
$('.add-more-row').click(function () {
var $repeatRow = $('.table12 tbody tr');
$repeatRow.after($repeatRow.clone());
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="row">
<div class="col-md-12">
<table class="table table-bordered table12">
<thead>
<tr>
<th></th>
<th>BODY PART(S):</th>
<th>AREA AFFECTED:</th>
<th>SPECIFIC AREA:</th>
<th>PAIN SCALE:</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>
<select class="form-control">
<option>Select</option>
<option>1</option>
</select>
</td>
<td>
<select class="form-control">
<option>Select</option>
<option>1</option>
</select>
</td>
<td>
<select class="form-control">
<option>Select</option>
<option>1</option>
</select>
</td>
<td>
<select class="form-control">
<option>Select</option>
<option>1</option>
</select>
</td>
</tr>
</tbody>
</table>
<button class="btn btn-primary add-more-row">
<i class="fa fa-plus"></i>
Add More Rows
</button>
</div>
</div>
我该如何解决?
解决方案
$('.add-more-row').click(function () {
var $repeatRow = $('.table12 tbody tr').last();
$repeatRow.after($repeatRow.clone());
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="row">
<div class="col-md-12">
<table class="table table-bordered table12">
<thead>
<tr>
<th></th>
<th>BODY PART(S):</th>
<th>AREA AFFECTED:</th>
<th>SPECIFIC AREA:</th>
<th>PAIN SCALE:</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>
<select class="form-control">
<option>Select</option>
<option>1</option>
</select>
</td>
<td>
<select class="form-control">
<option>Select</option>
<option>1</option>
</select>
</td>
<td>
<select class="form-control">
<option>Select</option>
<option>1</option>
</select>
</td>
<td>
<select class="form-control">
<option>Select</option>
<option>1</option>
</select>
</td>
</tr>
</tbody>
</table>
<button class="btn btn-primary add-more-row">
<i class="fa fa-plus"></i>
Add More Rows
</button>
</div>
</div>
你运行这个js,
$('.add-more-row').click(function () {
var $repeatRow = $('.table12 tbody tr');
$repeatRow.after($repeatRow.clone());
});
首先单击按钮,选择 1 行。
但首先,再次单击按钮,选择您添加的多行。
改变你的js。
$('.add-more-row').click(function () {
var $repeatRow = $('.table12 tbody tr').last();
$repeatRow.after($repeatRow.clone());
});
推荐阅读
- java - 当我尝试运行应用程序时,Flink 流应用程序 IllegalAccessError
- node.js - 如何过滤关系 - TypeORM
- reactjs - Material-UI Snackbar 未在 Firefox 中显示
- node.js - 未定义 docker-compose 服务器中的错误
- angular - 将模型从 Angular 映射到 asp.net 核心
- javascript - 如何从json中获取图像并显示它
- next.js - 在 NextJS 中导入使用“文档”的 CSS 辅助函数
- laravel - 在刀片模板文件中转义原始 html
- apple-watch - 在 watchOS 7 中的后台 URLSession 中下载数据
- javascript - 我无法使用 JavaScript 获得 Html.TextBoxFor 值