简体   繁体   English

Javascript中带有循环的奇怪行为

[英]Strange behaviour with loop in Javascript

I've got a table-like structure with text inputs in which I am trying to make an entire row to be removed with all their children, but first passing the values of cells up one by one in the rows below to keep IDs numbering structure. 我有一个带有文本输入的类似表格的结构,在该结构中,我试图将整个行与所有子级一起删除,但首先要在下面的行中逐行向上传递单元格的值,以保持ID编号结构。

The table structure is like this: 表结构如下:

<table cellpadding=0>
    <tr id="myRow1">
        <td id="#R1C1">
            <input class="myCell">
        </td>
        <td id="#R1C2">
            <input class="myCell">
        </td>
        <td id="#R1C3">
            <input class="myCell">
        </td>
        <td id="#R1C4">
            <input class="myCell">
        </td>
    </tr>
    <tr id="myRow2">
        <td id="#R2C1">
            <input class="myCell">
        </td>
        <td id="#R2C2">
            <input class="myCell">
        </td>
        <td id="#R2C3">
            <input class="myCell">
        </td>
        <td id="#R2C4">
            <input class="myCell">
        </td>
    </tr>
    <!-- ...and so on. -->
</table>

Having this table, when some event is triggered,I make this code run: 有了这个表,当某个事件被触发时,我使这段代码运行:

var rows = 1; // This value is updated when adding/removing a line

//This code runs from any <tr> by event keyup
if (event.altKey) { // I define here all Alt+someKey actions.
    // Getting position values
    var currentId = $(this).attr('id');
    var row = Number(currentId.split('C')[0].substring(1));
    var column = Number(currentId.split('C')[1]);
    var belowVal;

    if (event.which == 66) { //Case Ctrl+B
        // If not the last row, start looping
        if (rows > row) {
            var diff = rows - row;
            // Iteration over rows below
            for (var i = 0; i < diff; i++) {
                // Iteration over each column
                for (var c = 1; c <= 4; c++) {
                    // here I try to get the value from column below
                    belowVal = $('#R'+(row+1+i).toString() +
                              'C'+c.toString()).val();
                    $('#R'+(row+i).toString()+'C' +
                           c.toString()).find('.myCell').val(belowVal);
                }
            }
        }
        $('#myRow'+rows.toString()).empty();
        $('#myRow'+rows.toString()).remove();
        rows--;
    }
}

It works fine for removing the last row, but, when trying to remove an upper row, the values of current row and the ones below become blank instead of moving up. 它对于删除最后一行很好用,但是,当尝试删除上一行时,当前行和下面的值将变为空白而不是向上移动。 I made this code for each row below to pass it's values to the upper row, but it isn't doing what I wanted. 我为下面的每一行编写了此代码,以将其值传递给上一行,但是它没有执行我想要的操作。

Why is this happening? 为什么会这样呢? I couldn't figure it out. 我不知道。

The problem seem to be, that the ids you are using to access the values are not the ids of the input elements, but rather the ids of the containing table cells. 问题似乎在于,您用来访问值的id不是输入元素的id,而是包含表单元格的id。

Here an approach, which doesnt use the ids, but relies on the nodes structure instead, code not tested: 这是一种不使用id而是依靠节点结构的方法,未经测试的代码:

if (event.which == 66) {
    var currentrow = $(this);
    var currentinputs = currentrow.find('input.myCell');
    while(var nextrow = currentrow.next('tr')) {
        var nextinputs = nextrow.find('input.myCell');
        currentinputs.each(function(index,element){
            element.val(nextinputs.get(index).val());
        });
        currentrow = nextrow;
        currentinputs = nextinputs;
    }
    currentrow.remove();
}

RESOLVED 解决

Thanks to @JHoffmann, I was able to resolve my problem like this: 感谢@JHoffmann,我得以解决如下问题:

for (var c = 1; c <= 4; c++) {
    // here I try to get the value from column below
    belowVal = $('#R'+(row+1+i).toString()+'C'+c.toString())
               .find('.myCell').val();
    $('#R'+(row+i).toString()+'C'+c.toString())
               .find('.myCell').val(belowVal);
}

In the line that assigns a value to belowVal , I forgot to call the method .find('.myCell') before calling .val() . 在为belowVal分配值的belowVal ,我忘记了在调用.val()之前调用方法.find('.myCell') .val() That was the mistake that caused my code to fail, as @JHoffmann commented in his answer. 这就是导致我的代码失败的错误,正如@JHoffmann在他的回答中评论的那样。

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

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