You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Bootstrap 4 DataTables搜索分页功能失效,请求代码排查帮助

Fixing Bootstrap 4 DataTables Not Working Issues

我仔细检查了你的代码,找到了几个导致DataTables搜索、分页功能失效的关键问题,逐一修复后就能正常运行了:

你引入DataTables Bootstrap样式时,rel属性的单词拼错了——把stylesheet写成了sylesheet,这会导致样式文件加载失败,DataTables的UI元素自然显示不出来。

错误代码:

<link rel ="sylesheet" type ="text/css" href="https://cdn.datatables.net/1.10.19/css/dataTables.bootstrap4.min.css">

修复后:

<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.19/css/dataTables.bootstrap4.min.css">

2. Duplicate jQuery Imports Causing Conflict

你同时加载了两个不同版本的jQuery:Google CDN的3.4.1和jQuery官方的3.3.1。重复加载jQuery会覆盖全局的$对象,导致DataTables依赖的jQuery环境出现异常。

解决方案:删掉其中一个引用,推荐保留较新的3.4.1版本,移除这行:

<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>

3. Invalid Table Structure (Repeated <tbody> Tags)

你的PHP循环把<tbody>标签放在了while内部,这会导致每生成一行数据就创建一个新的<tbody>,不符合HTML表格的规范(一个表格只能有一个<tbody>),DataTables无法正确解析这种混乱的结构。

另外,你的表头列数和内容行的列数不一致(内容多了一个View按钮列),也会影响DataTables的解析,我一起帮你修正了:

修复后完整表格代码:

<table class="table table-striped table-bordered patientsTable" style="width: 100%">
<thead>
<tr>
<th scope="col">First Name</th>
<th scope="col">Surname</th>
<th scope="col">DOB</th>
<th scope="col">Address</th>
<th scope="col">Town</th>
<th scope="col">Postcode</th>
<th scope="col">Action</th> <!-- 添加对应按钮的表头 -->
</tr>
</thead>
<tbody>
<?php while($row = $result ->fetch_assoc()){
$rowid = $row['id'];
$firstname = $row['firstname'];
$surname = $row ['surname'];
$dob = $row['dob'];
$address = $row['address'];
$town = $row['town'];
$postcode = $row['postcode'];
echo"
<tr>
<td>$firstname</td>
<td>$surname </td>
<td>$dob</td>
<td>$address</td>
<td>$town</td>
<td>$postcode</td>
<td><a class='btn btn-danger' href='patientsmedication.php?editid=$rowid'>View</a></td>
</tr>";
} ?>
</tbody>
</table>

4. Verify Script Load Order (Already Correct)

你的脚本加载顺序是对的:jQuery → Popper.js → Bootstrap JS → DataTables Core → DataTables Bootstrap Adapter,这个顺序能保证依赖关系正常,不用调整。

完成以上修复后,刷新页面就能看到DataTables的搜索框、分页控件正常显示并工作了。

内容的提问来源于stack exchange,提问作者kmcs87

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 07:44:16