0

我想根据隐藏的字段重新排序,当它被隐藏时我将隐藏字段 zone zone_deleted 设置为 1 以将其标记为已删除。

function insertZone() {

    var table = document.getElementById('zoneItems');

    var e = document.getElementById('zone');
    var number = e.options[e.selectedIndex].value;
    var rowcount = document.getElementById('zoneItems').rows.length;

    if(rowcount == 0){
        var x = table.insertRow(-1);

        var head1 = x.insertCell(0);
        head1.innerHTML = "Zone";

        var head2 = x.insertCell(1);
        head2.innerHTML = "Motor Reference Point";

        var head3 = x.insertCell(2);
        head3.innerHTML = " ";

    }

        for (var i=0; i < number; i++)
        {
            var x = table.insertRow(-1);
            var a = x.insertCell(0);
            //a.innerHTML = "<input type='hidden' name='zone_id' value='' /> ";

            num = i+1;

            var b = x.insertCell(1);
            b.innerHTML = "<input type='text' name='zone_description[]' size='18' value=''/><input id='zone"+num+"' type='hidden' name='zone_deleted[]' value='0' />";

            var c = x.insertCell(2);
            c.innerHTML = "<button type='button' class='button lastChild' onclick='removeZone(this)' ><img src='themes/default/images/id-ff-clear.png'></button>";
        }

        //set the row ordering
        count();

}

function removeZone(rows) {

    var _row = rows.parentElement.parentElement;

    _row.cells[1].getElementsByTagName('input')[1].value = '1';

    document.getElementById('zoneItems').rows[_row.rowIndex].style.display = 'none';

    count();


}

function count(){ //sets the row ordering

    var table = document.getElementById("zoneItems");
    var tbody = table.tBodies[0];

    for (var i = 0, row; row = tbody.rows[i]; i++) { //loop through rows

        if(i != 0){//if not the first row

           // var deleted = row.cells[1].getElementsByTagName('input')[1].value;


            for (var j = 0, col; col = row.cells[j]; j++) {//loop through cols


                if(j == 0){//insert into only the first td
                    col.innerHTML = "<span>" +i+ "</span><input type='hidden' name='zone_id' value='' />";
                }

            }
        }

    }

}
4

1 回答 1

0

对显示的行号使用与用于索引 DOM 中的行的变量不同的变量。

function count() {
    var table = document.getElementById("zoneItems");
    var tbody = table.tBodies[0];
    var rownum = 1;

    for (var i = 1, row; row = tbody.rows[i]; i++) { //loop through rows
        var deleted = row.cells[1].getElementsByTagName('input')[1].value;
        if (deleted != "1") {
            row.cells[0].innerHTML = "<span>" +rownum+ "</span><input type='hidden' name='zone_id' value='' />";
            rownum++;
        }
    }
}

小提琴

于 2013-02-19T20:02:17.820 回答