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

如何用Bootstrap和Thymeleaf为模态框表单填充对应学生信息?

问题分析与解决方案

你遇到的问题核心原因很明确:HTML中ID必须是唯一的,但你在学生列表的循环里重复创建了ID为myModal的模态框,同时所有Edit按钮的data-target都指向同一个#myModal。浏览器只会识别第一个匹配该ID的元素,所以不管点击哪个Edit按钮,都会触发第一个学生的模态框。

下面给你两种解决方案,优先推荐第一种(更高效、易维护):


方案一:共用单个模态框,通过JavaScript动态填充数据

这种方式只创建一个模态框,点击Edit按钮时用JS把当前学生的数据填充到表单里,避免生成大量重复的DOM元素。

修改后的完整代码

<!DOCTYPE html> 
<html xmlns:th="http://www.w3.org/1999/xhtml" xmlns:sf="http://www.w3.org/1999/xhtml" lang="en"> 
<head> 
    <title>Student List</title> 
    <meta charset="utf-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> 
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
</head> 
<body> 
    <div class="container"> 
        <h1 style="text-align:center;">Students List</h1> 
        <table class="table table-striped"> 
            <tr> 
                <th>Id</th> 
                <th>Roll</th> 
                <th>First Name</th> 
                <th>Last Name</th> 
                <th>Age</th> 
                <th>Setting</th> 
            </tr> 
            <tr th:each="student : ${studentList}"> 
                <td th:text="${student.id}"></td> 
                <td th:text="${student.roll}"></td> 
                <td th:text="${student.firstName}"></td> 
                <td th:text="${student.lastName}"></td> 
                <td th:text="${student.age}"></td> 
                <td> 
                    <!-- 给Edit按钮添加data-*属性存储当前学生的所有数据 -->
                    <button type="button" class="btn btn-info btn-lg edit-student-btn" 
                            data-toggle="modal" data-target="#myModal"
                            data-id="${student.id}"
                            data-roll="${student.roll}"
                            data-firstname="${student.firstName}"
                            data-lastname="${student.lastName}"
                            data-age="${student.age}"
                            data-pass="${student.pass}">
                        Edit 
                    </button> 
                    <!-- 你的Delete按钮可以放在这里 -->
                </td> 
            </tr> 
        </table> 

        <!-- 单个模态框,移到循环外面 -->
        <div class="modal fade" id="myModal" role="dialog"> 
            <div class="modal-dialog"> 
                <div class="modal-content"> 
                    <div class="modal-header"> 
                        <button type="button" class="close" data-dismiss="modal">&times;</button> 
                        <h4 class="modal-title">Edit Student</h4>
                    </div> 
                    <div class="modal-body"> 
                        <legend>Edit Student Form</legend> 
                        <form class="form-horizontal" action="#" th:action="@{/doRegistration}" method="post"> 
                            <fieldset> 
                                <div class="form-group"> 
                                    <label class="col-md-4 control-label" for="roll">Roll</label> 
                                    <div class="col-md-4"> 
                                        <input id="edit-id" name="id" type="hidden" class="form-control input-md"/> 
                                        <input id="edit-roll" name="roll" type="text" placeholder="Roll" class="form-control input-md"/> 
                                        <span th:if="${existRoll !=null}" style="color:Red;"> Already Exist !</span> 
                                    </div> 
                                </div> 
                                <div class="form-group"> 
                                    <label class="col-md-4 control-label" for="firstName">First Name</label> 
                                    <div class="col-md-4"> 
                                        <input id="edit-firstName" name="firstName" type="text" placeholder="firstName" class="form-control input-md"/> 
                                    </div> 
                                </div> 
                                <div class="form-group"> 
                                    <label class="col-md-4 control-label" for="lastName">Last Name </label> 
                                    <div class="col-md-4"> 
                                        <input id="edit-lastName" name="lastName" type="text" placeholder="lastName" class="form-control input-md"/> 
                                    </div> 
                                </div> 
                                <div class="form-group"> 
                                    <label class="col-md-4 control-label" for="age">Age </label> 
                                    <div class="col-md-4"> 
                                        <input id="edit-age" name="age" type="text" placeholder="age" class="form-control input-md"/> 
                                    </div> 
                                </div> 
                                <div class="form-group"> 
                                    <label class="col-md-4 control-label" for="pass">Password</label> 
                                    <div class="col-md-4"> 
                                        <input id="edit-pass" name="pass" type="password" placeholder="password" class="form-control input-md"/> 
                                    </div> 
                                </div> 
                                <div class="form-group"> 
                                    <label class="col-md-4 control-label" for="register"></label> 
                                    <div class="col-md-4"> 
                                        <button id="register" name="register" class="btn btn-primary"> Save Changes </button> 
                                    </div> 
                                </div> 
                            </fieldset> 
                        </form> 
                    </div> 
                    <div class="modal-footer"> 
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    </div>
                </div> 
            </div> 
        </div>
    </div>

    <!-- 填充模态框数据的JS逻辑 -->
    <script>
        // 监听模态框即将显示的事件
        $('#myModal').on('show.bs.modal', function (event) {
            // 获取触发模态框的Edit按钮
            const button = $(event.relatedTarget);
            // 从按钮的data-*属性中提取学生数据
            const id = button.data('id');
            const roll = button.data('roll');
            const firstName = button.data('firstname');
            const lastName = button.data('lastname');
            const age = button.data('age');
            const pass = button.data('pass');
            
            // 把数据填充到模态框的表单里
            const modal = $(this);
            modal.find('#edit-id').val(id);
            modal.find('#edit-roll').val(roll);
            modal.find('#edit-firstName').val(firstName);
            modal.find('#edit-lastName').val(lastName);
            modal.find('#edit-age').val(age);
            modal.find('#edit-pass').val(pass);
        });

        // 保留你原有的模态框自动显示逻辑
        $(function () {
            [[${existRoll != null}]] && $('#myModal').modal('show');
        });
    </script>
</body> 
</html>

关键改动说明

  1. 把模态框从循环中移出来,只保留一个
  2. 给每个Edit按钮添加data-*属性,存储对应学生的ID、姓名等数据
  3. 利用Bootstrap模态框的show.bs.modal事件,在模态框显示前将按钮上的学生数据填充到表单中

方案二:为每个学生生成唯一的模态框ID

这种方式会给每个学生创建独立的模态框,通过动态ID匹配对应的按钮,适合简单场景但会生成大量DOM元素。

修改核心代码片段

<tr th:each="student : ${studentList}"> 
    <!-- 其他列保持不变 -->
    <td> 
        <!-- 动态设置data-target,指向当前学生的模态框 -->
        <button type="button" class="btn btn-info btn-lg" 
                data-toggle="modal" th:attr="data-target='#myModal-' + ${student.id}">
            Edit 
        </button> 

        <!-- 动态生成唯一的模态框ID -->
        <div class="modal fade" th:id="'myModal-' + ${student.id}" role="dialog"> 
            <div class="modal-dialog"> 
                <div class="modal-content"> 
                    <!-- 模态框内容和你原来的代码一致,表单数据直接用${student.xxx}填充 -->
                    <!-- ... 这里省略你原有的表单代码 ... -->
                </div>
            </div>
        </div>
    </td> 
</tr>

缺点说明

如果学生数量较多,页面会生成大量重复的模态框DOM结构,导致页面加载变慢、内存占用增加,所以更推荐方案一。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 09:32:39