简体   繁体   English

OnClicks链接到错误的按钮

[英]OnClicks links to wrong button

I'm trying to link the button across every row to delete that row when clicked. 我正在尝试在每一行上链接按钮,以在单击时删除该行。 However, every delete button is linked to the onclick delete of the last created row. 但是,每个删除按钮都链接到最后创建的行的onclick删除。

For example: 例如:

TABLE

Record 1 | 记录1 | deleteButton1 deleteButton1

Record 2 | 记录2 | deleteButton2 deleteButton2

Record 3 | 记录3 | deleteButton3 deleteButton3

Actions: 动作:

clicks deleteButton1 ---> deletes the row with "Record 3" 单击deleteButton1 ---> 删除带有“记录3”的行

clicks deleteButton1 ---> tries to delete the row with "Record 3" (aka nothing happens b/c row not found) 单击deleteButton1 ---> 尝试删除带有“记录3”的行 (也就是未找到未发生的b / c行)

clicks deleteButton2 ---> tries to delete the row with "Record 3" (aka nothing happens b/c row not found) 单击deleteButton2 ---> 尝试删除带有“记录3”的行 (也就是未找到未发生的b / c行)

HTML: HTML:

<table id="Table"></table>

JavaScript: JavaScript:

//Code snippet
for (var x = 0; x < itemArray.length; x++) 
{
    selectedItem = itemArray[x];
    table = document.getElementById("Table");
    row = table.insertRow(table.rows.length);
    cell1 = row.insertCell(0);
    cell2 = row.insertCell(1);

    cell1.innerHTML = selectedItem;
    cell2.innerHTML = "<button>—</button>";  //Delete button across every row.
    cell2.onclick = function () { removeRow(selectedItem); };
}

function removeRow(content, where) 
{ 
            var table;

            table = document.getElementById("Table");

            var iter;

            for (var i = 0; i < table.rows.length; i++) 
            {
                iter = table.rows[i].cells[0].innerHTML;

                if (iter == content)
                {
                    table.deleteRow(i);
                }
            }
}

You may need something like this : 您可能需要这样的东西:

http://www.codingforums.com/javascript-programming/170869-dynamically-add-delete-reorder-rows-table.html http://www.codingforums.com/javascript-programming/170869-dynamically-add-delete-reorder-rows-table.html

Here : 这里 :

       for (var i= startingIndex; i< tbl.tBodies[0].rows.length; i++) {

            // CONFIG: next line is affected by myRowObject settings
            tbl.tBodies[0].rows[i].myRow.one.data = count; // text

            // CONFIG: next line is affected by myRowObject settings
            tbl.tBodies[0].rows[i].myRow.two.name = INPUT_NAME_FS; // input text
            tbl.tBodies[0].rows[i].myRow.two.id = INPUT_NAME_FS + count;

            tbl.tBodies[0].rows[i].myRow.three.name = INPUT_NAME_FS_DESIGN; // input text
            tbl.tBodies[0].rows[i].myRow.three.id = INPUT_NAME_FS_DESIGN + count;


            // CONFIG: next line is affected by myRowObj settings

            // CONFIG: requires class named classy0 and classy1
            tbl.tBodies[0].rows[i].className = 'classy' + (count % 2);

            count++;
        }

Each onclick function references the variable selectedItem . 每个onclick函数都引用变量selectedItem After your for loop, that variable is set to the last item in the array. for循环之后,该变量将设置为数组中的最后一项。 So, every button will reference that last item. 因此,每个按钮都将引用最后一个项目。 Here is a demonstration . 这是一个示范

I suggest using Javascript's parentNode and rowIndex to allow a button to reference its own parent row. 我建议使用Javascript的parentNoderowIndex允许按钮引用其自己的父行。

In my example below, rowIndex returns the index number of the tr that is the parentNode for the clicked cell ( td ). 在下面的示例中, rowIndex返回tr的索引号,该索引号是被单击的单元格( td )的parentNode This index number can be used to remove a table row directly. 该索引号可用于直接删除表行。

cell2.onclick = function () {  removeRow(this.parentNode.rowIndex);  };

function removeRow(x) { 
    document.getElementById("Table").deleteRow(x);
}

Working Example (jsFiddle) 工作示例(jsFiddle)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM