使用JavaScript onclick添加表格行

Pre*_*nin 3 html javascript

我正在尝试使用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>
Run Code Online (Sandbox Code Playgroud)

我真的很喜欢,但是,我喜欢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);
}
Run Code Online (Sandbox Code Playgroud)

但它似乎没有做任何事情.我假设我应该处理代码,知道哪个输入比最后一个更好,id="last"但我不知道如何做到这一点.

The*_*pha 8

您可以使用以下内容实现相同的目标

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>
Run Code Online (Sandbox Code Playgroud)

JS:

function addField(n)
{
    var tr = n.parentNode.parentNode.cloneNode(true);
    document.getElementById('tbl').appendChild(tr);
}
Run Code Online (Sandbox Code Playgroud)

另外,删除idfrom input,因为它ID必须是唯一的,并且记住你在所有输入中都使用相同的名称,因此,名称应该被命名为 links[],keywords[]并将violationtype[]它们用作数组,但不确定你的情况.

一个例子.


the*_*eye 7

一个完整的工作示例 - 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>
Run Code Online (Sandbox Code Playgroud)

现在,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);
    }
Run Code Online (Sandbox Code Playgroud)