过去两天以来,我对实现一种允许用户执行以下步骤的简单方法感到困惑:
- 用户有一个包含条目的表格
- 他可以点击每行的编辑按钮
- 模态视图打开
- 用户输入值
- 点击“保存”按钮后,这些值以及表中的 ID 将传输到我的 Controller
到目前为止我所取得的成就是:
我能够在用户单击编辑之后和模式窗口打开之前过滤掉单击的条目
$(this).closest('tr').find('.userID').text()
或
$(button.relatedTarget).attr('id')
但是我无法将必要的 userID 移交给我的 updateModal() JS 函数。我需要 Spring MVC Controller 的 ID。
当我尝试在 updateUser() 函数中获取这些参数时,它们就消失了。因此,在用户单击编辑按钮和显示模式之间,我丢失了这些信息。它们仅在单击编辑按钮后立即可用,稍后就不能使用。
我需要的是一种在模态视图打开后使这些参数可用的方法,并且当用户单击发送时我可以将用户ID发送到 Controller 。
我找到了这个选项:
localStorage.setItem('key', 'value');
但这对我没有帮助,因为我的 updateUser 函数不知道模式正在使用什么 ID。
这是我的 JS:
function updateUser() {
alert("try getting parameter");
alert($(this).closest('tr').find('.userID').text()); //not working HELP pls
alert($(button.relatedTarget).attr('id')); //not working too
alert("success?");
var newValues = {};
newValues["newName"] = $("#newName").val();
newValues["newAge"] = $("#newAge").val();
var userID = "???";
$.ajax({
type: "POST",
contentType: "application/json",
url: "/api/edit/" + userID,
data: JSON.stringify(newValues),
dataType: 'json',
timeout: 100000,
success: function (data) {
console.log("SUCCESS: ", data);
display("SUCCESS");
resultObj = data.result;
},
error: function (e) {
console.log("ERROR: ", e);
display("ERROR");
//display(2);
},
done: function (e) {
console.log("DONE");
display("DONE");
enableSearchButton(true);
}
});
}
这是我的 HTML:
<!--- start table -->
<div class="container">
<br>
<h3>Overview</h3>
<br>
<div class="container" id="modal-submit">
<div class="col-12">
<table class="table table-hover">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Age</th>
<th scope="col">ID</th>
</tr>
</thead>
<tbody>
<!--/*@thymesVar id="productList" type="java.util.List"*/-->
<tr id="person" th:each="person : ${elementList}">
<td th:text="${person.getName()}" id="username"></td>
<!--/*@thymesVar id="getTradableBTC" type="java.lang.Double"*/-->
<td th:text="${person.getAge()}" th:id="${person.getName()+person.getAge()}" id="age"></td>
<!--/*@thymesVar id="getTop" type="java.lang.Double"*/-->
<td th:text="${person.getId()} " th:id="${person.getName()+person.getId()}" class="userID"></td>
<td>
<div class="btn-toolbar" role="toolbar">
<!-- Button trigger modal -->
<div class="btn-group mr-2" role="group" aria-label="First group">
<button type="button" class="btn btn-outline-primary btn-sm" data-toggle="modal"
data-target="#myModal" data-th-id="${person.getId()}">
Edit
</button>
</div>
<div class="btn-group mr-2" role="group" aria-label="Second group">
<button type="button" class="btn btn-outline-danger btn-sm" id="delete">Delete
</button>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<button class="btn btn-primary" type="button" id="addElement">Add Element</button>
<button class="btn btn-light" type="button" id="DeleteAll">Delete all Elements</button>
</div>
</div>
<br>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog"
data-keyboard="false" data-backdrop="static">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Change data</h5>
<button type="button" class="close" data-dismiss="modal"
aria-label="Close"><span
aria-hidden="true">×</span></button>
</div>
<div class="row">
<div class="col-sm-6">
<div class="modal-body">
<input name="referencia" id="newName" type="text"
class="form-control"
placeholder="Enter new name">
<br>
<input name="referencia" id="newAge" type="text"
class="form-control"
placeholder="Enter new age">
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-light"
data-dismiss="modal">Discard
</button>
<button type="button" class="btn btn-primary"
onclick="updateUser()">Save
</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
最佳答案
解决您的问题的一种可能的解决方案是
- 当用户点击表格行时,获取相应行的 UserID,即
$(this).closest('tr').find('.userID').text()
将上面检索到的用户 ID 存储到模型 DOM
var userId = $(this).closest('tr').find('.userID').text(); $("#myModal").data('currentUserId', userId);
在
updateUser
中从 DOM 获取用户 ID。var userId = $("#myModal").data('currentUserId');
基本上,您需要将 UserID 存储在全局上下文中,一旦模型关闭,您就可以访问该 UserID,因为 updateUser
和表行单击都将在不同的上下文中调用。
此外,我认为您需要阅读这篇文章 https://stackoverflow.com/a/3127440/2509344了解 this
在 JavaScript 中的工作原理。
关于javascript - HTML JS/JQuery : Store Parameter after button click to use it for a later function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49522315/