|
 |
|
ิ สอบถามหน่อยค่ะ เขียน javascript checkbox checkall แยก table ยังไงค่ะ |
|
 |
|
|
 |
 |
|
ใส่ id table ด้วยครับ
*ไม่ได้เทส เขียนไกด์ให้ตาม code เดิมเท่านั้น
Code (PHP)
$i=0;
while(table){
<table id="<?=$i++;?>">
<tr>
<th><input type="checkbox" name="checkall" id="checkall" onchange="fncCheck(<?=$i?>)"></th>
<th>name</th>
</tr>
while(tr.td){
<tr>
<td><input type="checkbox" name="check[]"></td>
<td><?= $name;?></td>
</tr>
}
</table>
}
fncCheck(_id){
var checkboxes = $(this).closest('#'+_id).find(':checkbox');
checkboxes.prop('checked', $(this).is(':checked'));
}</script>
|
ประวัติการแก้ไข 2022-09-15 17:19:23
 |
 |
 |
 |
Date :
2022-09-15 17:18:48 |
By :
mongkon.k |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เวลาเขียน ถาม อย่าย่อ เพราะรู้คนเดียวเลยครับ ให้คนอื่นเขารู้ ที่มาที่ไปด้วย ก็จะเป้นกุศล สร้างตัวอย่างให้คนใหม่ๆ ได้เรียนรู้ด้วยครับ
ลองดูตัวอย่างนี้ครับ
Code (PHP)
<?php
$tables=[
[
['id'=>1, 'name'=>'table1 row1'],
['id'=>2, 'name'=>'table1 row2'],
['id'=>3, 'name'=>'table1 row3'],
] ,
[
['id'=>1, 'name'=>'table2 row1'],
['id'=>2, 'name'=>'table2 row2'],
['id'=>3, 'name'=>'table2 row3'],
]
];
foreach($tables as $i=>$tb){ ?>
<table id="<?=$i+1?>">
<thead>
<tr>
<th><input type="checkbox" id="<?=$i+1?>" onchange="fncCheck(this)"></th>
<th>id</th>
<th>name</th>
</tr>
</thead>
<tbody>
<?php foreach($tb as $tr){ ?>
<tr>
<td><input type="checkbox" name="check_<?=$tr['id']?>[]"></td>
<td><?= $tr['id']?></td>
<td><?= $tr['name']?></td>
</tr>
<?php } ?>
</table>
<?php } ?>
<script>
// base on jquery framework
var fncCheck=obj=>{
let isCheck = $(obj).prop('checked');
let tbody=$(obj).closest('thead').next();
tbody.find('checkbox').prop('checked', isCheck);
};
</script>
|
ประวัติการแก้ไข 2022-09-20 14:03:00
 |
 |
 |
 |
Date :
2022-09-20 14:01:02 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 03
|