需要一些帮助。 Javascript不是我的强项。我尝试循环遍历每个迭代本身递增1的以下元素ID。例如,c4_r1_cell_1将变为c4_r2_cell_2,c4_r3_cell_3等,每个循环通过。在进入下一个循环之前,我需要在每次迭代中使用相同的增量编号进行6次。在哪里看到" + loop_count +"每次进入下一个循环运行之前,它必须是相同的数字,直到它到达最后一个循环。
我不需要的是增加我的每个" + loop_count +"就像它们是单独的迭代一样,它增加了它们1,2,3,4,5,6。我需要它们是1,1,1,1,1,1然后是下一个循环迭代2,2,2,2,2,2等。
正在生成的HTML:
<td><input type="text" id="c1_r<?php echo $i; ?>_cell_2" name="c1_r<?php echo $i; ?>_cell_2" value="<?php echo $row2['c1_r'.$i.'_cell_2']; ?>" /></td>
<td><input type="text" id="c2_r<?php echo $i; ?>_cell_3" name="c2_r<?php echo $i; ?>_cell_3" value="<?php echo $row2['c2_r'.$i.'_cell_3']; ?>" /></td>
<td><input type="text" id="c3_r<?php echo $i; ?>_cell_4" name="c3_r<?php echo $i; ?>_cell_4" value="<?php echo $row2['c3_r'.$i.'_cell_4']; ?>" /></td>
<td><input type="text" id="c4_r<?php echo $i; ?>_cell_5" name="c4_r<?php echo $i; ?>_cell_5" value="<?php echo $row2['c4_r'.$i.'_cell_5']; ?>" /></td>
<td><input type="text" id="c5_r<?php echo $i; ?>_cell_6" name="c5_r<?php echo $i; ?>_cell_6" value="<?php echo $row2['c5_r'.$i.'_cell_6']; ?>" /></td>
这就是我现在所做的不起作用:
var loop_count = 1;
while (loop_count < 50) {
document.getElementById("c4_r" + loop_count + "_cell_" + loop_count + "") =
document.getElementById("c5_r" + loop_count + "_cell_" + loop_count + "");
document.getElementById("c5_r" + loop_count + "_cell_" + loop_count + "") = '';
loop_count++;
}
答案 0 :(得分:1)
您需要获取并设置您选择的DOM元素的innerHTML。就目前而言,您正在尝试选择整个DOM元素并将其分配给另一个DOM元素 - 这是您无法做到的。但是,您可以获取此元素的innerHTML值并分配它们。
var loop_count = 1;
while (loop_count < 50) {
document.getElementById("c4_r" + loop_count + "_cell_" + loop_count).value = document.getElementById("c5_r" + loop_count + "_cell_" + loop_count) ? document.getElementById("c5_r" + loop_count + "_cell_" + loop_count).value : '';
document.getElementById("c5_r" + loop_count + "_cell_" + loop_count).value = '';
loop_count++;
}
如果需要检查DOM元素是否存在,可以使用简单的if语句:
var loop_count = 1;
while (loop_count < 50) {
if (document.getElementById("c4_r" + loop_count + "_cell_" + loop_count)) {
document.getElementById("c4_r" + loop_count + "_cell_" + loop_count).value = document.getElementById("c5_r" + loop_count + "_cell_" + loop_count) ? document.getElementById("c5_r" + loop_count + "_cell_" + loop_count).value : '';
}
document.getElementById("c5_r" + loop_count + "_cell_" + loop_count) ? document.getElementById("c5_r" + loop_count + "_cell_" + loop_count).value = '' : undefined;
loop_count++;
}
答案 1 :(得分:0)
我修改了一下以给出一个上下文,希望底层逻辑应该是您正在寻找的:jsfiddle下面是一个简单的Javascript片段:
var loop_count = 1; //*variable initialization*
while(loop_count<50){ //*iterator and the condition*
var text=document.getElementById("c4_r" + loop_count + "_cell_" + loop_count + "").innerHTML; // *assigning to a a new variable the content of the element matching the ID*
loop_count++; } //variable increment to meet stop iteration when at 50 and closure of the while.
答案 2 :(得分:-1)
试试这种方式。它可以帮助你
var loop_count = 1;
while (loop_count < 50) {
$('#c4_r' + loop_count + '_cell_' + loop_count + '').val($('#c5_r' + loop_count + '_cell_' + loop_count + '').val());
$('#c5_r' + loop_count + '_cell_' + loop_count + '').val('');
loop_count++;
}