如果选中复选框,则更改颜色-onchange事件

Change color if checkbox is checked - onchange event

本文关键字:颜色 -onchange 事件 复选框 如果      更新时间:2023-09-26

如果选中复选框,我将尝试更改div的背景色。

它适用于循环外的div,但不适用于循环内的div。所有的变量看起来都很好,所以我认为这是一个语法错误。

谢谢!

<input type="checkbox" name="check0" value='1' <?php echo ((${executed_mod0}=="1")? "checked" : ''); ?> onchange='this.nextSibling.style.backgroundColor = this.checked ? "#6EDBFF" : "white";'><input type="text" name="procedure0" value="<?php echo $repair_mod0;?>">
<?php   
$i=1;
while($i<$nrpm)
{
echo '<br><input type="checkbox" name="check'.$i.'" value="1"'. ((${executed_mod.$i}=="1")? "checked":"").' onchange="this.nextSibling.style.backgroundColor = this.checked ? '"#6EDBFF'" : '"white'";"><input type="text" name="procedure'.$i.'" value="'.${repair_mod.$i}.'">';
$i++;
};
?>

也许这可以让你朝着正确的方向前进。在获取要设置背景颜色的文本框时,我又添加了一个.nextSibling

HTML

<input type="checkbox" name="check0" value='1' onchange='setColor(this)' />
<input type="text" name="procedure0" value="1" />
<input type="checkbox" name="check1" value='2' onchange='setColor(this)' />
<input type="text" name="procedure1" value="2" />
<input type="checkbox" name="check2" value='3' onchange='setColor(this)' />
<input type="text" name="procedure2" value="3" />

JavaScript

function setColor(ele){
    ele.nextSibling.nextSibling.style.backgroundColor = ele.checked ? "#6EDBFF" : "white";   
}