我无法获取上一项的值并相应地更新下一项,不确定我该怎么做。我也尝试过:last-child
,但似乎没有返回所需的值。
if ($("tbody").children().length == 0) {
var item = `<tr>
<th scope="row">1</th>
<td>
<p>This is a paragraph</p>
</td>
<td>
<button type="button" class="btn btn-success" style="margin-right:1rem;">Complete</button>
<button type="button" class="btn btn-light">Edit</button>
</td>
</tr>`
$("tbody").append(item);
} else {
var lastValue = parseInt($("tbody th:last-of-type").html());
item = `<tr><th scope="row">` + (lastValue + 1) + `</th>
<td>
<p>This is a para</p>
</td>
<td>
<button type="button" class="btn btn-success" style="margin-right:1rem;">Complete</button>
<button type="button" class="btn btn-light">Edit</button>
</td></tr>`
$("tbody").append(item);
}
答案 0 :(得分:0)
我使用:last-of-type
代替了:last
,它似乎返回了正确的值,并且我能够相应地更新行数。
if ($("tbody").children().length == 0) {
var item = `<tr>
<th scope="row">1</th>
<td>
<p>This is a paragraph</p>
</td>
<td>
<button type="button" class="btn btn-success" style="margin-right:1rem;">Complete</button>
<button type="button" class="btn btn-light">Edit</button>
</td>
</tr>`
$("tbody").append(item);
} else {
var lastValue = parseInt($("tbody th:last").html());
item = `<tr><th scope="row">` + (lastValue + 1) + `</th>
<td>
<p>This is a para</p>
</td>
<td>
<button type="button" class="btn btn-success" style="margin-right:1rem;">Complete</button>
<button type="button" class="btn btn-light">Edit</button>
</td></tr>`
$("tbody").append(item);
}