用下面的代碼我只是想選擇/取消選擇表格行中的所有複選框,通常這個jquery代碼在靜態表格中工作正常,但不能在動態表格中工作用foreach
生成。我真的沒有發現我實際上錯過了什麼,因爲我是新來的。選擇/取消選擇不在動態表格行中的所有複選框
的jQuery:
$('#chkall').click(function(event) { //on click
if(this.checked) { // check select status
$('.TID').each(function() { //loop through each checkbox
this.checked = true; //select all checkboxes with class "checkbox1"
});
}else{
$('.TID').each(function() { //loop through each checkbox
this.checked = false; //deselect all checkboxes with class "checkbox1"
});
}
});
查看網頁:
<form role="form" action="<?php echo base_url(); ?>attendance/statusUpdate/" method="post" name="attendance" id="attendance">
<table class='table table-condensed table-bordered table-hover text-center' cellspacing='0' cellpadding='0' width='100%' id='tabledata'>
<thead>
<tr class='info'>
<th class="text-center">Sl#</th>
<th class="text-center">Trainee ID</th>
<th class="text-center">Trainee Name</th>
<th class="text-center">Date</th>
<th class="text-center">In Time</th>
<th class="text-center">Out Time</th>
<th class="text-center">Status</th>
<th class="text-center">Withdraw</th>
<?php
if (isset($userinfo) and ($userinfo->Role == 1)) {
?>
<th class="text-center">Allow <input type='checkbox' name='chkall' id='chkall'></th>
<th class="text-center">Remarks</th>
<?php
}
?>
</tr>
</thead>
<tbody>
<tr>
<td colspan="10">Session # </td>
</tr>
<tr>
<td>Faculty:</td>
<td colspan="2"> </td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<?php
if (isset($userinfo) and ($userinfo->Role == 1)) {
?>
<td></td>
<td></td>
<?php
}
?>
</tr>
<?php
$i = 0;
foreach($attendance as $row){
$i++;
if ($row->Status=='Late-In' or $row->attnDate == null) {
$style='red';
}else{
$style='black';
}
?>
<tr style="color:<?php echo $style?>"><td><?=$i?></td>
<td><?=str_pad($row->TraineeID, 7, "0", STR_PAD_LEFT)?></td>
<td class="text-left"><?=$row->Name?></td>
<td><?=$row->attnDate?></td>
<td><?=$row->inTime?></td>
<td><?=$row->outTime?></td>
<td><?=$row->Status?></td>
<td><?=$row->Reason?></td>
<?php
if (isset($userinfo) and ($userinfo->Role == 1)) {
?>
<td>
<input type='checkbox' name='TraineeID[]' class="TID" id="TID" value="<?=$row->TraineeID ?>"
<?php if($row->Status==='Late-In')
{
?>
unchecked>
<?php
}else if($row->Status==='Present'){
?>
checked>
<?php
}
?>
</td>
<td>
<input type="text" name="remarks[]" id="remarks">
</td>
<?php
}
?>
<?php
}
?>
</tr>
</tbody>
<?php
}
?>
</table>
</form>
什麼是$出勤的價值?複選框是否正確顯示? – musafar006
'$ attendance'是數組,並正確顯示覆選框。 –
您不能將相同的ID「TID」分配給多個輸入框。 – goseo