上下文
我有一条GET
路由,它加载一个.ejs文件,该文件根据传递的变量accounts
的长度向表中添加数据行。这里的accounts
是带有键_id, type, nickname, rewards, balance, customer_id
的字典数组。
<% if (accounts.length > 0) { %>
<% accounts.forEach(account => { %>
<tr class="blue-grey lighten-3 account-row">
<th scope="row" class="account-id"><%= account._id %></th>
<td><%= account.type %></td>
<td><%= account.nickname %></td>
<td><%= account.rewards %></td>
<td>$<%= account.balance %></td>
<td><%= account.customer_id %></td>
<td>
<span class="table-remove">
<button type="button" class="btn btn-danger btn-rounded btn-sm my-0 remove" data-toggle="modal" data-target="#removeAccountModal">
Remove
</button>
</span>
</td>
<div class="modal fade" id="removeAccountModal" tabindex="-1" role="dialog" aria-labelledby="removeAccountModalLabel"
aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="removeAccountModalLabel">You are about to remove this account</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
CAUTION!!!
<br>
<br>
Removing this account will delete all the data associated with it. You must visit a local branch to retrieve any monies left residing in the account.
</div>
<div class="modal-footer">
<button type="button" class="btn grey darken-1" data-dismiss="modal">Cancel</button>
<form id="remove-form" action="/" method="POST">
<button id="removeAccount" class="btn btn-primary" type="submit">Remove Account</button>
</form>
</div>
</div>
</div>
</div>
</tr>
<% }) %>
问题
每行数据上都有一个删除按钮,用于打开Bootstrap模式,确认用户确实希望删除该行的数据。在以模式提交后,这将以DELETE
元素的形式发起一个form
请求。
我想传递所选行的关联account._id
变量,以便可以将form
的{{1}}属性修改为action
。
我无法访问模式的父窗口DOM元素。
我的jQuery代码如下:
/<%= account._id %>/?_METHOD=DELETE
尝试的方法:
<script>
$(document).ready(function () {
console.log("loaded and ready to go!"); // works as expected
var $remove = $(".remove"); // works as expected
console.log($remove.length); // works as expected
$(".remove").click(() => {
console.log("I'm in the modal!") // works as expected
var $parent = $(this).parent().closest("tr"); // find row
console.log($parent); // w.fn.init [prevObject: w.fn.init(0)]
var $account_id = $parent.find("th"); // find account id
console.log($account_id); // w.fn.init [prevObject: w.fn.init(0)]
console.log($account_id.text()); // returns blank type string
$("#remove-form").prop("action", "/" + $account_id + "?
_method=DELETE"); // change action attribute to account id value
})
});
</script>
答案 0 :(得分:0)
您已将模态置于CASE WHEN {type}='Kit/Package'
THEN min(({memberitem.quantityonhand}/NULLIF({memberquantity},0))/(NULLIF({memberitem.preferredStockLevel}/NULLIF({memberquantity},0),0)))
ELSE
CASE WHEN {type}='Inventory Item'
THEN {quantityonhand}/NULLIF({preferredstocklevel},0)
ELSE 'nope'
END
ELSE 'nope'
END
循环中。因此,您只有一个模式,而每一行只有一个。这里的问题是,您为所有这些人分配了相同的ID forEach
。尝试为每个模式指定唯一的ID,例如:
removeAccountModal
,然后在每个删除按钮上将数据目标替换为
<div class="modal fade" id="<%= account._id %>" tabindex="-1" role="dialog" aria-labelledby="removeAccountModalLabel" aria-hidden="true">
因此,您具有独特的模态,现在您可以将data-target="#<%= account._id %>"
直接放在action属性中
答案 1 :(得分:0)
感谢@dimitris tseggenes为我指出正确的方向。
在此,每个模态div的唯一id
势在必行。事实证明id
属性值不能仅由数字组成。
我重构了id
和target-data
属性,使其分别如下所示:
id="account<%= account._id %>id"
data-target=#account"<%= account._id %>id"
我从这篇文章的回复中学到了以下内容: