php jquery选择while fetch_array中的特定复选框

时间:2012-12-11 10:57:34

标签: php jquery css checkbox

我在选择特定复选框时遇到问题,并在mysql_fetch_array中为其div设置higlight css

这是我的代码

$count=1;
$query = mysql_query('SELECT * FROM thread');
while($row = mysql_fetch_array($query))
{
  echo "<div class='row".$count."'><input type='checkebox' class='chk_box".$count."'> ".$row['title']."</div>";
  $count++; 
}

<script>
 var count= "<?php echo $count?>";
 for(var y=1;y<=count;y++){ 
$('.chk_box'+y).click(function() {
  if(this.checked) {
    $('.row'+y).addClass('backcolor');
  }
  else{
    $('.row'+y).removeClass('backcolor');
  }
 });
        }
</script>

1 个答案:

答案 0 :(得分:1)

修正了som拼写错误,添加了DOM就绪函数,并稍微更改了jQuery,试试吗?

$count=1;
$query = mysql_query('SELECT * FROM thread');
while($row = mysql_fetch_array($query)) {
   echo "<div class='row".$count."'><input type='checkbox' class='chk_box".$count."'> ".$row['title']."</div>";
   $count++; 
}

<script type="text/javascript">
$(function() {
    $('[class^="chk_box"]').on('click', function() {
        $(this).closest('div').toggleClass('backcolor', this.checked);
    });
});
</script>​​​​​​​​​​

作为旁注,在for循环中附加事件处理程序通常不是一个好主意。