正确编写代码 - Javascript

时间:2018-05-22 15:52:15

标签: javascript php html

大家好我有一张桌子,显示我网站上注册的所有用户,动态选中复选框没问题,我报告了我的桌子代码:

php:

<!--DELETE DATA CHECKBOX-->
<?php
session_start();
include '../a/connessione.php';
if(isset($_POST['delete'])){
$checkbox = $_POST['check'];
for($i=0;$i<count($checkbox);$i++){
$del_id = $checkbox[$i]; 
$db ="DELETE FROM user WHERE id='".$del_id."'";
$q = mysqli_query($connessione, $db);
}
}
?>
<!--/DELETE DATA CHECKBOX-->

表:

<div class="px-4">  
<div class="table-wrapper">
<!--Table-->
<table class="table table-hover mb-0">
<!--Table head-->
<thead>
<tr>   
<th scope="row">   
</th>  
<th class="th-lg"><a>ID </a></th>
<th class="th-lg"><a href="">Nome</a></th>
<th class="th-lg"><a href="">Email</a></th>
<th class="th-lg"><a href="">Azioni</a></th>
</tr>
</thead>
<!--Table head-->

<!--Table body-->
<?php
$i=0;
while($row = mysqli_fetch_assoc($query)){ ?>

<tbody>
<tr>
<th scope="row">
<input name="check[]" type="checkbox" id="<?php echo $row['id'] ;?>" value="<?php echo $row['id'] ;?>">
<label   for="<?php echo $row['id'] ;?>" class="label-table"></label>
</th>
<td><?php echo $row['id'] ;?><br></td>
<td><?php echo $row['nome'] ;?><br></td>
<td><?php echo $row['email'] ;?><br></td>
<td><a href="#edit<?php echo $row['id']; ?>" data-toggle="modal" class="btn btn-outline-blue btn-rounded btn-sm px-2"> <i class="fa fa-info-circle mt-0"></i></a>  
<?php include('button.php'); ?></td>
</tr>
</tbody>  
<?php } ?>   
<!--Table body-->
</table>
<!--Table-->
</div>

<hr class="my-0">

<!--Bottom Table UI-->
<div class="d-flex justify-content-between">

una foto:

enter image description here

我的目标是创建一个选中所有其他点击的复选框,我带了正确的代码来执行此操作

代码:

<input type="checkbox" class="js-selectall" data-target=".js-selectall1" />

<table>
  <tr>
    <td><input type="checkbox" class="js-selectall1" /> </td>
    <td><input type="checkbox" class="js-selectall1" /> </td>
    <td><input type="checkbox" class="js-selectall1" /> </td>
    <td><input type="checkbox" class="js-selectall1" /> </td>
  </tr>
  <tr>
    <td><input type="checkbox" class="js-selectall1" /> </td>
    <td><input type="checkbox" class="js-selectall1" /> </td>
    <td><input type="checkbox" class="js-selectall1" /> </td>
  </tr>
</table>

我无法正确编写代码,并允许我创建一个复选框,选中所有其他点击,同时正确编写两个代码,我该怎么办?

我的尝试:

<table class="table table-hover mb-0">
<!--Table head-->
<thead>
<tr>



<th scope="row">
<input type="checkbox" class="js-selectall" data-target=".js-selectall1" />
<label   for="#" class="label-table"></label>

</th>

<th class="th-lg"><a>ID </a></th>
<th class="th-lg"><a href="">Nome</a></th>
<th class="th-lg"><a href="">Email</a></th>
<th class="th-lg"><a href="">Azioni</a></th>
</tr>
</thead>
<!--Table head-->

<!--Table body-->
<?php
$i=0;
while($row = mysqli_fetch_assoc($query)){ ?>

<tbody>
<tr>
<th scope="row">
<input name="check[]" class="js-selectall1" type="checkbox" id="<?php echo $row['id'] ;?>" value="<?php echo $row['id'] ;?>">
<label   for="<?php echo $row['id'] ;?>" class="label-table"></label>
</th>
<td><?php echo $row['id'] ;?><br></td>
<td><?php echo $row['nome'] ;?><br></td>
<td><?php echo $row['email'] ;?><br></td>
<td><a href="#edit<?php echo $row['id']; ?>" data-toggle="modal" class="btn btn-outline-blue btn-rounded btn-sm px-2"> <i class="fa fa-info-circle mt-0"></i></a>  
<?php include('button.php'); ?></td>
</tr>
</tbody>  
<?php } ?>   
<!--Table body-->
</table>
<!--Table-->
</div>


<script>
$('.js-selectall').on('change', function() {
  var isChecked = $(this).prop("checked");
  var selector = $(this).data('target');
  $(selector).prop("checked", isChecked);
});
</script>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

0 个答案:

没有答案