使用 JavaScript 点击添加表格行
Add table row with JavaScript onclick
我正在尝试使用javascript添加下面完全相同的元素。我已经尝试了这里找到的所有解决方案,我什至试图用php echo
来回应该元素,但没有运气。无需更改任何输入名称或类似名称,只需单击该按钮,即可在表中添加另一行,仅此而已。
这是元素:
<tr>
<td><input type="text" name="links"></td>
<td><input type="text" name="keywords"></td>
<td><input type="text" name="violationtype"></td>
<td><input type="submit" id="last" class="button" value="Add another line" onclick:"addField();"></td>
</tr>
我真的愿意做任何事情,但是,我想要javascript,因为我在没有任何外部引用(例如jquery)的情况下保持我的系统,但在这一点上,我愿意接受任何建议。我尝试使用以下代码执行此操作:
function addFields() {
var number = document.getElementById("last").value;
var html = '<tr>' +
'<td><input type="text" name="links"></td>' +
'<td><input type="text" name="keywords"></td>' +
'<td><input type="text" name="violationtype"></td>' +
'<td><input type="submit" id="last" class="button" value="Add another line" name="line" onclick:"addField();"></td>';
number.appendChild(html);
}
但它似乎什么也没做。我认为我应该以比id="last"
更好的方式处理代码,知道哪个输入是最后的,但我不知道该怎么做。
您可以使用
以下方法实现相同的目标
.HTML:
<table id="tbl">
<tr>
<td><input type="text" name="links" /></td>
<td><input type="text" name="keywords" /></td>
<td><input type="text" name="violationtype" /></td>
<td><input type="submit" class="button" value="Add another line" onclick="addField(this);" /></td>
</tr>
</table>
.JS:
function addField(n)
{
var tr = n.parentNode.parentNode.cloneNode(true);
document.getElementById('tbl').appendChild(tr);
}
另外,从input
中删除id
,因为ID
必须是唯一的,并且请记住您对所有输入使用相同的名称,因此,名称应命名为links[]
,keywords[]
和violationtype[]
将它们用作数组,但不确定您的情况如何。
举个例子。
一个完整的工作示例 - http://jsfiddle.net/QmHdL/
表可以像这样创建
<table id="myTable">
<tr>
<td><input type="text" name="links"></td>
<td><input type="text" name="keywords"></td>
<td><input type="text" name="violationtype"></td>
<td><input type="button" class="button" value="Add another line" onclick="addField();"></td>
</tr>
</table>
现在,addField
必须像这样实现
function addField (argument) {
var myTable = document.getElementById("myTable");
var currentIndex = myTable.rows.length;
var currentRow = myTable.insertRow(-1);
var linksBox = document.createElement("input");
linksBox.setAttribute("name", "links" + currentIndex);
var keywordsBox = document.createElement("input");
keywordsBox.setAttribute("name", "keywords" + currentIndex);
var violationsBox = document.createElement("input");
violationsBox.setAttribute("name", "violationtype" + currentIndex);
var addRowBox = document.createElement("input");
addRowBox.setAttribute("type", "button");
addRowBox.setAttribute("value", "Add another line");
addRowBox.setAttribute("onclick", "addField();");
addRowBox.setAttribute("class", "button");
var currentCell = currentRow.insertCell(-1);
currentCell.appendChild(linksBox);
currentCell = currentRow.insertCell(-1);
currentCell.appendChild(keywordsBox);
currentCell = currentRow.insertCell(-1);
currentCell.appendChild(violationsBox);
currentCell = currentRow.insertCell(-1);
currentCell.appendChild(addRowBox);
}
这里有一些问题。
应onclick:
onclick=
然后你附加到 id 'last' 的元素,这是一个输入元素 - 你不能附加到它。 如果你想添加一行,给你的表一个 ID 并附加到它,否则创建一个其他元素 -div/span 和一个 ID 并附加到它,如果 whle 表是从头开始创建的 bee
然后 - 你不能像这样附加 HTML;你需要创建一个新的表行元素并附加它或使用 [element].innerHTML += [your new row HTML]
。
相关文章:
- 如何将表格中的文本添加到使用按钮输入的文本区域
- 如何将JS应用于用户在表格中添加的行
- 在 AngularJS 表格中添加的行上显示 3 秒的弹出窗口
- 提交表单时如何在 HTML 表格中添加行
- 将工作表复制到新的电子表格,将单元格值添加到新电子表格名称的末尾
- 当我单击添加新按钮(WORDPRESS,PHP)时,我需要在表格中隐藏
- 向HTML页面添加按钮,更新表格信息
- 在不使用200行var的情况下,在结果字段中添加顺序命名的变量?[PDF表格]
- 从谷歌电子表格中向谷歌日历添加快速添加事件
- 在提交表格时IE8赢得了't将来自输入的属性以外的属性添加到请求中
- jQuery将文本添加到放置在表格单元格中的图像中
- Javascript- 将点击事件添加到动态生成的表格单元格中
- 为智能手机的 HTML 表格添加动画效果
- 在页面之间移动时向表格添加新行
- 防止从电子表格添加到日历时出现重复事件
- 将联系人表格添加到我的Joomla网站
- 向表格添加水平顶部滚动条的Aurelia方式
- Material Design Lite表格-添加行动态打断格式
- Google Apps脚本-使用侧边栏表单向电子表格添加一行
- 向2个表格添加滚动条