复选框:
多选的时候,在复选框的位置 , name 属性要写成 “xxx[ ]”形式,PHP才能接收多个值(以数组的形式接收多个值),否则只有最后一个值
多选:
querySelector() ,querySelectorAll()
querySelector() 方法返回文档中匹配指定 CSS 选择器的一个元素。
如果你需要返回所有的元素,请使用querySelectorAll() 方法替代
注意: querySelector() 方法仅仅返回匹配指定选择器的第一个元素。
.checked
if(ele.checked === true){undefined
alert(‘被选中’)
}
二、ele.checked ===false
if(ele.checked === false){undefined
alert(‘没被选中’)
}
<?php
header("content-type:text/html;charset:utf-8");
$url="mysql:host=mysql;dbname=database_lesson_28577_14_11226";
$user="lesson_28577_14_11226";
$pwd="5a409adfbec378cd9dd3acf13ede872a";
$conn = new PDO($url,$user,$pwd);
if(isset($_GET["id"])){
$conn->exec("delete from mail where id={$_GET["id"]}+0");
if($conn>0){
echo "<scrtip>alert('删除成功');window.location='mail.php'</scrtip>";
}
}
if(isset($_GET["name"])){
foreach($_GET["name"] as $value){
$conn->exec("delete from mail where id={$value}+0");
}
}
$shuju1 = $conn->query("select * from mail")->fetchAll(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
html,body{
padding: 0;
margin: 0px;
width: 100%;
height: 100%;
background: #f2f2f2;
}
#body{
height: 100%;
width: 960px;
background: #fff;
margin: 0 auto;
}
form>div{
border: 1px solid ;
margin-top: 20px;
}
#quanxuan{
display: flex;
}
.youjian{
display: flex;
}
.youjian>div{
margin-right: 30px;
}
.youjian>div:nth-child(2){
width: 100px;
padding-left: 5px;
text-align: left;
text-overflow:ellipsis;
overflow: hidden;
white-space: nowrap;
}
.youjian>div:nth-child(3){
width: 270px;
padding-left: 5px;
text-align: left;
text-overflow:ellipsis;
overflow: hidden;
white-space: nowrap;
}
</style>
<script>
window.onload=function(){
let a = false;
var all = document.querySelector("#all");
var others = document.querySelectorAll(".dan");
all.onclick = function(){
a = !a;
if(a){
others.forEach(val =>{
val.checked = true;
})
}else{
others.forEach(val =>{
val.checked = false;
})
}
}
}
</script>
</head>
<body>
<div id="body">
<form action="mail.php" >
<div id="quanxuan">
<input type="checkbox" id="all"/>
<a href="mail.php"><input type="submit" value="删除"/></a>
</div>
<?php foreach($shuju1 as $value){ ?>
<div class="youjian">
<input type="checkbox" name="name[]" value="<?php echo $value["id"]?>" class="dan"/>
<div><?php echo $value["title"] ?></div>
<div><?php echo $value["content"] ?></div>
<div><?php echo $value["time"] ?></div>
<a href="mail.php?id=<?php echo $value["id"] ?>"><div>删除</div></a>
</div>
<?php } ?>
</form>
</div>
</body>
</html>