我正在尝试实现如图所示的动态增长/收缩表。我知道我需要使用 insertRow()
函数,但我对如何动态地为行提供 ID 感到困惑。如果选中复选框,我需要能够禁用结束日期输入字段(这就是为什么需要提供 ID)。我需要能够插入行和删除行。我在编程概念方面相当有经验,但总体上对 JavaScript 和 Web 开发不熟悉。如果有人可以指出示例代码或解释是否有另一种有效的方法,我将不胜感激。
最佳答案
一个没有 id 的例子,适用于每一行控制, 就像您的屏幕截图一样(id 只是其中的一种方式...)
你不能有多个相同的id,那么 假设您的操作按钮由它们各自的类名调用, “.add”和“.del”
用于删除
$(".del").on("click", function()
{
// removing the line of element clicked
$(this).parents("tr").remove();
});
换行
$(".add").on("click", function()
{
var line = $(this).parents("tr"); // get the line of element clicked
var lineOffset = line.index(); // get the offset position of this line
// and using css selector, you can simply add line after another
$("table tr:eq("+lineOffset+")").after(line.clone(true));
// line.clone(true) is an example, but you can put directly your html like "<tr>.... what you want</tr>"
});
表格测试
<table>
<tr id="a_0"><td>test0</td><td><span class="del">[X]</span><span class="add">[o]</span></td></tr>
<tr id="a_1"><td>test1</td><td><span class="del">[X]</span><span class="add">[o]</span></td></tr>
<tr id="a_2"><td>test2</td><td><span class="del">[X]</span><span class="add">[o]</span></td></tr>
</table>
(function() {
$(".del").on("click", function() {
// removing the line of element clicked
$(this).parents("tr").remove();
});
$(".add").on("click", function() {
var line = $(this).parents("tr"); // get the line of element clicked
var lineOffset = line.index(); // get the offset position of this line
// and using css selector, you can simply add line after another
$("table tr:eq(" + lineOffset + ")").after(line.clone(true));
// line.clone(true) is an example, but you can put directly your html like "<tr>.... what you want</tr>"
});
})()
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<table>
<tr id="a_0">
<td>test0</td>
<td><span class="del">[X]</span><span class="add">[o]</span>
</td>
</tr>
<tr id="a_1">
<td>test1</td>
<td><span class="del">[X]</span><span class="add">[o]</span>
</td>
</tr>
<tr id="a_2">
<td>test2</td>
<td><span class="del">[X]</span><span class="add">[o]</span>
</td>
</tr>
</table>
但是,您可以在我的示例中看到,ID 以 a_* 开头 未使用(是的,根据您的情况,这不是必需的和相对的) 另一种方法是使用 jquery 方法 .index() 单击线偏移并..删除或复制它!
注意:
如果您确实需要使用线路 ID, 好吧,您可以继续使用这样的 css 选择器:
$("tr[id^='a_']")
IF EMPTIED TABLE
$(".del").on("click", function()
{
// removing the line of element clicked
$(this).parents("tr").remove();
if($("table tr").length == 1) // the only one remaining is the hidden_control (if you doesn't use a external button but a row)
$("#hidden_control").show(); // or .css("display", "block");
});
$("#hidden_control").on("click", function()
{
$("table").append("<tr><td>...</tr>"); // add a new first line
$(this).hide(); // and hide it directly until next reinit
});
// hidden button at top (or bottom) of table (not in the table)
<input type="button" id="hidden_control" value="Refill new data">
// or, hidden row solution (where colspan=6 depend the number of cell you have:
<tr id='hidden_control'><td colspan='6'><button>Refill new data</button></td></tr>
// CSS class for hidden_control
#hidden_control
{ display: none; }
文档:
继续 https://api.jquery.com/ ,并搜索“parents”、“after”、“remove”、“append”、“html”、“index”
关于javascript - 动态插入/删除表行(包括如何为添加的行提供 ID),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34378069/