Javascript HTMLJS/JQuery:在单击按钮后存储参数,用于以后的函数
从过去两天开始,我对实现一种简单的方法来允许用户执行以下步骤感到困惑: 用户有一个包含条目的表 他可以单击编辑按钮上的每行 将打开一个模态视图 用户输入值 单击“保存”按钮,这些值加上表中的id将传输到我的控制器 到目前为止,我取得的成就是: 我能够在用户单击“编辑”之后和模式窗口打开之前过滤出单击的条目Javascript HTMLJS/JQuery:在单击按钮后存储参数,用于以后的函数,javascript,jquery,html,Javascript,Jquery,Html,从过去两天开始,我对实现一种简单的方法来允许用户执行以下步骤感到困惑: 用户有一个包含条目的表 他可以单击编辑按钮上的每行 将打开一个模态视图 用户输入值 单击“保存”按钮,这些值加上表中的id将传输到我的控制器 到目前为止,我取得的成就是: 我能够在用户单击“编辑”之后和模式窗口打开之前过滤出单击的条目 $(this).closest('tr').find('.userID').text() 或 但是我无法将必要的用户ID交给我的updateModal JS函数。我需要Spring MVC控
$(this).closest('tr').find('.userID').text()
或
但是我无法将必要的用户ID交给我的updateModal JS函数。我需要Spring MVC控制器的ID
当我试图在updateUser函数中获取这些参数时,它们就消失了。因此,在用户点击编辑按钮和显示模式之间,我丢失了这些信息。它们仅在单击“编辑”按钮后立即可用,但不能在以后使用
我需要的是一种在模态视图打开后使这些参数可用的方法,并且当她的用户单击send时,我可以将userID发送给控制器
我发现这个选项:
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存储在全局上下文中,一旦模型关闭,您就可以访问它,因为updateUser和table row click都将在不同的上下文中调用
此外,我认为您需要阅读本文以了解JavaScript中的工作原理。我想特别感谢您不仅提供了解决方案,而且您还提供了一个额外的链接,我了解了我的问题所在并提高了我的理解。太好了!
var userId = $("#myModal").data('currentUserId');