PHP/jQuery/AJAX实现动态行数表单自动拉取MySQL数据
动态行表单的AJAX自动填充实现方案
你现在逐行手写5个事件绑定块的核心问题,是完全依赖硬编码的ID作为事件匹配规则,只要新增行,新元素的ID没有被绑定规则覆盖,就必须重复写绑定逻辑,冗余度极高。正确的实现思路是用统一类名做事件代理,不需要关心新增了多少行,一次绑定全量生效。
第一步:调整HTML结构的统一类标识
保留原有行靠数字后缀区分ID的规则即可,只需要给两类核心元素加统一类名,用来做事件匹配,不用靠ID逐行找元素:
- 所有行的废弃物编码输入框,统一增加类名
waste-code-input - 所有行的自动填充容器(原
#wasteDiv1这类元素)统一增加类名waste-fetch-target
调整后的单行HTML示例:
<div id="materialContainer"> <!-- 单行结构模板,新增行复制后修改数字后缀即可 --> <div id="MaterialGroup1" class="row mx-0 p-0 mt-1"> <input id="wasteCode1" class="form-control success mr-1 waste-code-input" name="wasteCode1" style="width:150px;height:32px;"> <div id="wasteDiv1" class="wasteCodeDiv waste-fetch-target" style="display:inherit;"> <input id="material1" type="text" name="material1" class="form-control mr-1" style="width:150px;height:32px;"> <input id="classification1" name="classification1" class="form-control mr-1" style="width:300px;height:32px;"> <input id="baseCost1" name="baseCost1" type="number" class="form-control mr-1" style="width:85px;text-align:right;" onblur="calcSubtotal(this)"> </div> <!-- 下方的按钮、其他输入框结构和原有逻辑完全一致,无需修改 --> <button type="button" id="btnAddPallets1" name="addPallets" class="btn btn-outline-success mr-1 mt-0 p-0" style="height:32px; width:100px; font-weight:800;color:white;" data-toggle="modal" data-target="#addPalletModal">+</button> <input id="totalPallets1" name="totalPallets1" type="number" class="form-control mr-1" style="width:100px;text-align:right;"> <input id="wasteCodeTotalWeight" name="wasteCodeTotalWeight" type="text" class="form-control mr-1" style="width:100px;text-align:right;" autocomplete="off"> <input id="subtotal1" name="subtotal1" type="text" class="form-control mr-1" style="width:100px;text-align:right;background-color:transparent !important;"> <select id="receiveType1" name="receiveType1" class="form-control success mr-1" style="width:125px;height:32px;"> <option selected disabled hidden value=''></option> <option>Process</option> <option>RTS</option> </select> <button type="button" id="btnAddMaterial1" class="btn btn-outline-warning mr-1 mt-0 p-0" style="height:32px; width:44px; font-weight:800;color:white;" onclick="addMaterial()">+</button> <input type="text" id="timestamp1" name="timestamp1" class="form-control mr-1" style="width:200px;border:1px solid red;visibility:hidden;"> </div> </div>
注意:原有代码里的material输入框重复写了两次
id="material1",记得删掉重复的ID属性,避免DOM解析异常。
第二步:重写jQuery事件绑定,替代逐行手写的冗余代码
用jQuery的事件代理机制,把事件绑定在document层级,匹配所有带waste-code-input类的元素——不管是页面初始加载就存在的行,还是后来动态新增的行,只要元素带了对应的类名,就会自动触发事件,不需要写循环、不需要为每一行单独绑定:
<script> jQuery(document).ready(function(){ // 统一绑定所有废弃物编码输入框的keyup事件,完全替代原来5个重复的绑定块 jQuery(document).on('keyup', '.waste-code-input', function(){ const $currentInput = jQuery(this); // 从当前输入框的ID中提取行号,也就是原来传给后端的divNum const rowNum = $currentInput.attr('id').replace('wasteCode', ''); // 定位到当前行对应的填充容器 const $targetContainer = jQuery('#wasteDiv' + rowNum); // 发起请求的逻辑和原有实现完全一致,不需要调整 $targetContainer.load("waste-code.php", { wasteID: $currentInput.val().trim(), divNum: rowNum }); }); }) </script>
你原有的addMaterial()新增行逻辑只需要做一个小调整:新增的废弃物编码输入框加上waste-code-input类,就会自动适配上面的事件绑定,不需要在新增行时再额外写事件绑定代码。不管你新增10行还是100行,整个JS代码都不需要修改。
第三步:优化后端代码的安全性(可选但强烈建议)
你现在的PHP代码直接把前端传入的wasteID拼接到SQL语句中,存在SQL注入风险,改成预编译语句的写法不会改变原有输出逻辑,但安全性会大幅提升:
<?php require 'php/includes/dbh.inc.php'; mysqli_select_db($conn, "RC Terminal"); $wasteID = trim($_POST['wasteID']); $divNum = intval($_POST['divNum']); // 用预编译语句替代直接拼接SQL的写法 $stmt = mysqli_prepare($conn, "SELECT Material, Classification, Base_Cost FROM Materials WHERE Waste_Code = ?"); mysqli_stmt_bind_param($stmt, "s", $wasteID); mysqli_stmt_execute($stmt); $result = mysqli_stmt_get_result($stmt); if (mysqli_num_rows($result) > 0){ // Waste_Code是唯一值,直接取第一行结果即可,不需要循环遍历 $row = mysqli_fetch_assoc($result); // 对输出内容做转义,避免特殊字符破坏HTML结构 $material = htmlspecialchars($row['Material']); $classification = htmlspecialchars($row['Classification']); $baseCost = htmlspecialchars($row['Base_Cost']); echo '<input id="material' . $divNum . '" type="text" name="material' . $divNum . '" class="form-control mr-1" style="width:150px;height:32px;" value="' . $material . '">'; echo '<input id="classification' . $divNum . '" name="classification' . $divNum . '" class="form-control mr-1" style="width:300px;height:32px;" value="' . $classification . '">'; echo '<input id="baseCost' . $divNum . '" name="baseCost' . $divNum . '" type="number" class="form-control mr-1" style="width:85px;text-align:right;" value="' . $baseCost . '">'; } else { echo '<input id="material' . $divNum . '" type="text" name="material' . $divNum . '" class="form-control mr-1" style="width:150px;height:32px;">'; echo '<input id="classification' . $divNum . '" name="classification' . $divNum . '" class="form-control mr-1" style="width:300px;height:32px;">'; echo '<input id="baseCost' . $divNum . '" name="baseCost' . $divNum . '" type="number" class="form-control mr-1" style="width:85px;text-align:right;">'; } die();
常见踩坑提醒
- 不要在每次新增行的时候重新给输入框绑定事件,那样会造成事件重复触发,用document层级的事件代理是最稳定的方案
- 只要新增行的元素ID遵循
wasteCode{行号}、wasteDiv{行号}的命名规则,整个JS逻辑就不需要做任何修改,自动适配 - 如果觉得keyup事件触发太频繁(每敲一个字符就发一次请求),可以加300ms的防抖逻辑,等用户停止输入后再发请求,减少服务器压力,这个属于体验优化,不是必须修改的项
内容的提问来源于stack exchange,提问作者dkoukol
相关产品推荐
相关产品推荐

