php - 如何修复复选框的 jquery removeClass?(添加课堂作品)
问题描述
身体(许多代码都取自 youtube 上的指南,一切正常,我以同样的方式做了所有事情,但它不起作用)。removeClass 仅在我刷新页面时才有效
<body>
<div class="container">
<div class="todo">
<h1>todo app with php</h1>
<h3>add a new to do</h3>
<form action="index.php" method="POST">
<div class="form-group">
<input type="text" class="form-control" name="todo" placeholder="todo name">
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary" value="add a new todo task list">
</div>
</form>
<?php while ($row = mysqli_fetch_array($result)) { ?>
<div class="todo-item">
<?php if ($row['checkbox']) { ?>
<input type="checkbox" class="check-box" data-todo-id="<?php echo $row['id']; ?>" checked />
<h2 class="checked"><?php echo $row['t_name'] ?></h2>
<?php } else { ?>
<input type="checkbox" data-todo-id="<?php echo $row['id']; ?>" class="check-box" />
<h2><?php echo $row['t_name'] ?></h2>
<?php } ?>
<small>created: <?php echo $row['t_date'] ?></small>
<!--<button><a href="index.php?delete_todo=<?php echo $row['id']; ?>" class=" btn btn-danger">delete</a></button> -->
</div>
<?php } ?>
</div>
</div>
在脚本中,也许我应该输入类似 e.stopPropagation() 的内容,但我不明白在哪里以及如何输入。addClass 有效,但我不能回头删除选中的类。
<script>
$(document).ready(function() {
// checkbox toggle function
$(".check-box").on('click', function(e) {
//Get Task id from Task Table using checkbox
const id = $(this).attr('data-todo-id');
$.post('check.php', {
id: id
},
(data) => {
if (data != 'error') {
const h2 = $(this).next();
if (data === '1') {
h2.removeClass('checked');
// If Data Response id 1 means if checkbox is checked this alert will be show
} else {
h2.addClass('checked');
// If Data Response id 0 means if checkbox is unchecked this alert will be show
}
}
}
);
});
});
</script>
我有数据的 check.php
<?php
if (isset($_POST['id'])) {
require 'db.inc.php';
// Get Id From Post Method
$id = $_POST['id'];
if (empty($id)) {
echo 'error';
} else {
// Select Task From Info table
$record = mysqli_query($connection, "SELECT * FROM todo WHERE id=$id");
$todo = mysqli_fetch_array($record);
$uId = $todo['id'];
$checked = $todo['checkbox'];
$uChecked = $checked ? 0 : 1;
//Update Task Completed Status
$res = mysqli_query($connection, "UPDATE todo SET checkbox=$uChecked WHERE id=$uId");
if ($res) {
echo $checked;
} else {
echo "error";
}
$connection = null;
exit();
}
} else {
header("Location: ../index.php?mess=error");
}
解决方案
推荐阅读
- python - ModuleNotFoundError:没有名为“Turtle”的模块
- firebase - firebase 仅部署功能无法识别我的任何功能
- python - 如何让主要功能使用另一个功能?
- reactjs - 使用 react-three-fiber 创建全景 360 网络
- python - 为什么 Django 查询两个 DateTimeFields 相等性不起作用?
- flask - Cloud Run on Anthos 上的 Istio 授权?
- javascript - 从解析的文本输入创建嵌套 json
- python - 如何使用 python 从不同大小的列表中附加熊猫数据框?
- verilog - 编写 RTL 逻辑的问题
- python - 添加两个不同大小的图像或填充白色像素以使其在 Python 中更大