jQuery只影响while循环中的最后一个结果:MySQL

我正在尝试为while循环中的每一行中的每个已检查项目进行背景颜色更改。 目前,jQuery仅适用于最后一行。 我确实在输入id中放了一个$i变量,但老实说,我不知道该怎么办。 我尝试了这个 , .each函数,以及Stack Overflow上的一堆答案,但我无法弄清楚如何处理这个问题。

这是jQuery:

 $(document).ready(function(){ $('#org[$i]').change(function(){ if(this.checked){ $(this).siblings('table').removeClass('unchecked'); $(this).siblings('table').addClass('checked'); $(this).parentsUntil('td').removeClass('unchecked'); $(this).parentsUntil('td').addClass('checked'); } else { $(this).siblings('table').removeClass('checked'); $(this).siblings('table').addClass('unchecked'); $(this).parentsUntil('td').removeClass('checked'); $(this).parentsUntil('td').addClass('unchecked'); } }); }); 

这是循环(切出一些不重要的东西)。 $i在每个#org正确迭代(我在Firebug中检查过):

 if ($i % 4 == 0) echo ($i > 0? '' : '') . ''; echo " 
//blah, blah, blah
'; if ($i == $countRows - 1) echo ''; $i++;

你的问题是你试图匹配像org[1]这样的id作为PHP的输出,但是你正在使用带有[$i]的jQuery选择器 – 由于一些原因,这不起作用。

什么将工作是这样的jQuery选择器:

 $('input[id^="org\["]') 

这将选择id为以org[开头]的输入元素。 我在括号前面放置了一个退格区,因为jQuery对括号有一个特殊含义,这“逃脱”了这个特殊含义。

你为什么不用CSS呢?

 input[type="checkbox"] + label { // Whatever you want to do } input[type="checkbox"]:checked + label { // Whatever you want to do. } 

这是一个简单的小提琴,显示了这一点。

在你的JavaScript中你使用$ i但它是pp变量。 实际上你可以为此目的使用class not id

 ';} 

在javascript中

 $('.dummy').change(function(e){ //Do something that you need});