
本文旨在解决在使用Ajax动态填充表格时,由于选择器不精确导致Ajax请求在所有行上重复执行的问题。通过修改jQuery选择器,实现每个表格行独立触发和处理Ajax请求,确保数据的准确性和用户体验。本文将提供详细的代码示例和步骤,帮助你理解和解决此类问题。
在动态生成的表格中使用Ajax,一个常见的问题是事件绑定到多个元素上,导致不必要的重复请求。例如,当你在一个表格的每一行都有一个下拉选择框,并且希望根据选择的内容动态更新另一个下拉框时,如果选择器不精确,更改一个选择框的值可能会触发所有行的Ajax请求。以下是如何解决这个问题的详细步骤:
原代码中,.category-dropdown 选择器绑定了 change 事件,并且在 success 回调中,使用 .sub-category-dropdown 选择器更新所有子类别下拉框。这导致每次更改任何一个类别下拉框,所有的子类别下拉框都会被更新,从而产生了重复执行的问题。
关键在于找到当前触发 change 事件的类别下拉框所在行的子类别下拉框。可以使用 jQuery 的 closest() 和 find() 方法来实现:
$(document).ready(function() {
$('.category-dropdown').on('change', function() {
var category_id = this.value;
// 精准定位当前行的子类别下拉框
const subSelect = $(this).closest("tr").find(".sub-category-dropdown");
$.ajax({
url: "fetch-subcategory-by-category.php",
type: "POST",
data: {
category_id: category_id
},
cache: false,
success: function(result) {
// 只更新当前行的子类别下拉框
subSelect.html(result);
}
});
});
});代码解释:
结合 HTML 和 JavaScript,一个完整的示例可能如下所示:
<table class="center" id="Cateogry">
<tr>
<th>Category</th>
<th>Item</th>
</tr>
<tr>
<td>
<div class="form-group">
<label for="CATEGORY-DROPDOWN">Category</label>
<select class="form-control category-dropdown">
<option value="">Select Category</option>
<?php
require_once "../config.php";
$result = mysqli_query($con,"SELECT * FROM menu_category where outlet_id = 18");
while($row = mysqli_fetch_array($result)) {
?>
<option value="<?php echo $row['category_id'];?>"><?php echo $row["category_name"];?></option>
<?php
}
?>
</select>
</div>
</td>
<td style="width:30%">
<div class="form-group">
<label for="SUBCATEGORY">Sub Category</label>
<select class="form-control sub-category-dropdown">
</select>
</div>
</td>
</tr>
<tr>
<th>Category</th>
<th>Item</th>
</tr>
<tr>
<td>
<div class="form-group">
<label for="CATEGORY-DROPDOWN">Category</label>
<select class="form-control category-dropdown">
<option value="">Select Category</option>
<?php
require_once "../config.php";
$result = mysqli_query($con,"SELECT * FROM menu_category where outlet_id = 18");
while($row = mysqli_fetch_array($result)) {
?>
<option value="<?php echo $row['category_id'];?>"><?php echo $row["category_name"];?></option>
<?php
}
?>
</select>
</div>
</td>
<td style="width:30%">
<div class="form-group">
<label for="SUBCATEGORY">Sub Category</label>
<select class="form-control sub-category-dropdown">
</select>
</div>
</td>
</tr>
</table>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
$('.category-dropdown').on('change', function() {
var category_id = this.value;
const subSelect = $(this).closest("tr").find(".sub-category-dropdown");
$.ajax({
url: "fetch-subcategory-by-category.php",
type: "POST",
data: {
category_id: category_id
},
cache: false,
success: function(result) {
subSelect.html(result);
}
});
});
});
</script>注意:
通过使用 $(this).closest("tr").find(".sub-category-dropdown"),我们能够精准地定位到当前行对应的子类别下拉框,从而避免了不必要的重复请求,提高了程序的效率和用户体验。这个技巧在处理动态生成的表格数据时非常有用,特别是在需要对每一行进行独立操作的场景下。
以上就是解决Ajax在表格行中重复执行的问题:精准定位选择器的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号