如何使用 DOM 更改 HTML 表格第一行中的按钮? (通过 DOM 添加和删除行)

发布于 2024-12-23 03:44:19 字数 1678 浏览 0 评论 0原文

在下面的小提琴中,我尝试使用 DOM 添加和删除表行。根据脚本,每行的最后一个字段将有一个删除按钮。但我希望第一个字段中的删除按钮是表外部的添加按钮。

只有第一行的按钮才应该是添加行按钮。该行的其余部分应该有删除按钮。我该怎么做?

jsfiddle - http://jsfiddle.net/7AeDQ/33/

Javascript

function deleteRow(row)
{
    var i=row.parentNode.parentNode.rowIndex;
    document.getElementById('POITable').deleteRow(i);
}


function insRow()
{
    console.log( 'hi');
    var x=document.getElementById('POITable');
    var new_row = x.rows[1].cloneNode(true);
    var len = x.rows.length;
    new_row.cells[0].innerHTML = len;

    var inp1 = new_row.cells[1].getElementsByTagName('input')[0];
    inp1.id += len;
    inp1.value = '';
    var inp2 = new_row.cells[2].getElementsByTagName('input')[0];
    inp2.id += len;
    inp2.value = '';
    x.appendChild( new_row );
} 

HTML

<div id="POItablediv">
            <input type="button" id="addmorePOIbutton" value="Add More POIs" onclick="insRow()"/>
    <table id="POITable" border="1">
        <tr>
            <td>POI</td>
            <td>Latitude</td>
            <td>Longitude</td>
            <td>Delete?</td>

        </tr>
        <tr>
            <td>1</td>
            <td><input size=25 type="text" id="latbox"/></td>
            <td><input size=25 type="text" id="lngbox" readonly=true/></td>
            <td><input type="button" id="delPOIbutton" value="Delete" onclick="deleteRow(this)"/></td>

        </tr>
    </table>

Here in the below fiddle, i am trying to add and remove table rows using DOM. As per the script the last field of every row will have a delete button. But i want the delete button in the first field to be the add button which is outside the table.

Only the button in the first row should be add row button. rest of the row should have delete button as it is. How can i do this ?

jsfiddle - http://jsfiddle.net/7AeDQ/33/

Javascript

function deleteRow(row)
{
    var i=row.parentNode.parentNode.rowIndex;
    document.getElementById('POITable').deleteRow(i);
}


function insRow()
{
    console.log( 'hi');
    var x=document.getElementById('POITable');
    var new_row = x.rows[1].cloneNode(true);
    var len = x.rows.length;
    new_row.cells[0].innerHTML = len;

    var inp1 = new_row.cells[1].getElementsByTagName('input')[0];
    inp1.id += len;
    inp1.value = '';
    var inp2 = new_row.cells[2].getElementsByTagName('input')[0];
    inp2.id += len;
    inp2.value = '';
    x.appendChild( new_row );
} 

HTML

<div id="POItablediv">
            <input type="button" id="addmorePOIbutton" value="Add More POIs" onclick="insRow()"/>
    <table id="POITable" border="1">
        <tr>
            <td>POI</td>
            <td>Latitude</td>
            <td>Longitude</td>
            <td>Delete?</td>

        </tr>
        <tr>
            <td>1</td>
            <td><input size=25 type="text" id="latbox"/></td>
            <td><input size=25 type="text" id="lngbox" readonly=true/></td>
            <td><input type="button" id="delPOIbutton" value="Delete" onclick="deleteRow(this)"/></td>

        </tr>
    </table>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

生活了然无味 2024-12-30 03:44:19

您可以通过克隆现有行(和现有按钮)来创建新行。如果您希望第一行中的按钮行为与其他行不同,则需要在 insRow 函数中覆盖更改后的行为(就像当前对输入 id 所做的那样)。

尝试将代码更改为:

HTML:

...
<tr>
    <td>1</td>
    <td><input size=25 type="text" id="latbox"/></td>
    <td><input size=25 type="text" id="lngbox" readonly=true/></td>
    <td><input type="button" id="delPOIbutton" value="Add More POIs" onclick="insRow()"/></td>
</tr>
...

JS:

function deleteRow(evt) {
    var i = evt.target.parentNode.parentNode.rowIndex;
    document.getElementById('POITable').deleteRow(i);
}


function insRow() {
    var x = document.getElementById('POITable');
    var new_row = x.rows[1].cloneNode(true);
    var len = x.rows.length;

    new_row.cells[0].innerHTML = len;

    var inp1 = new_row.cells[1].getElementsByTagName('input')[0];
    inp1.id += len;
    inp1.value = '';

    var inp2 = new_row.cells[2].getElementsByTagName('input')[0];
    inp2.id += len;
    inp2.value = '';

    var button = new_row.cells[3].getElementsByTagName('input')[0];
    button.value = "Delete row";
    button.onclick = function(it) {deleteRow(it)};

    x.appendChild( new_row );    
}

You create new rows by cloning an existing row (and existing button). If you want the behaviour of the button in the first row to be different from other rows, you need to override that changed behaviour in your insRow function (as you do for input ids currently).

Try changing your code to:

HTML:

...
<tr>
    <td>1</td>
    <td><input size=25 type="text" id="latbox"/></td>
    <td><input size=25 type="text" id="lngbox" readonly=true/></td>
    <td><input type="button" id="delPOIbutton" value="Add More POIs" onclick="insRow()"/></td>
</tr>
...

JS:

function deleteRow(evt) {
    var i = evt.target.parentNode.parentNode.rowIndex;
    document.getElementById('POITable').deleteRow(i);
}


function insRow() {
    var x = document.getElementById('POITable');
    var new_row = x.rows[1].cloneNode(true);
    var len = x.rows.length;

    new_row.cells[0].innerHTML = len;

    var inp1 = new_row.cells[1].getElementsByTagName('input')[0];
    inp1.id += len;
    inp1.value = '';

    var inp2 = new_row.cells[2].getElementsByTagName('input')[0];
    inp2.id += len;
    inp2.value = '';

    var button = new_row.cells[3].getElementsByTagName('input')[0];
    button.value = "Delete row";
    button.onclick = function(it) {deleteRow(it)};

    x.appendChild( new_row );    
}
临走之时 2024-12-30 03:44:19

我可以猜测您的表将动态生成,这就是您遇到问题的原因。

一个 jQuery 解决方案,正如您标记的那样:

$(function(){
    $("#POITable tr").eq(1).find('td:last').html('<input type="button" id="addmorePOIbutton" value="Add More POIs" onclick="insRow()"/>');
});

演示: http://jsfiddle.net /7AeDQ/59/

此外,您的删除按钮会出现多次,因此不要像您使用的那样使用固定的id

<input type="button" id="delPOIbutton" value="Delete" onclick="deleteRow(this)"/>

而是使用class或使用增量 id 类似id="delPOIbutton-1", id="delPOIbutton-2"

I can guess your table will be generated dynamically, that is why you having the issue.

A jQuery solution, as you have tagged it:

$(function(){
    $("#POITable tr").eq(1).find('td:last').html('<input type="button" id="addmorePOIbutton" value="Add More POIs" onclick="insRow()"/>');
});

Demo: http://jsfiddle.net/7AeDQ/59/

Also your delete button will appear multiple times so don't use a fixed id for it as you used:

<input type="button" id="delPOIbutton" value="Delete" onclick="deleteRow(this)"/>

Rather use a class or use incremental id like id="delPOIbutton-1", id="delPOIbutton-2"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文