2012-12-11 63 views
0

我有一個問題,選擇特定的複選框,並把higlight CSS其格的mysql_fetch_arrayPHP jQuery的選擇,而裏面的具體複選框fetch_array

這裏裏面是我的代碼

$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

固定索姆錯別字,添加了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循環中附加事件處理程序通常不是一個好主意。