[英]Modal Is Deleting The First Row In The Table Incorrectly. How Do I get the right ID associated with my DELETE button?
I've been at this all day.我整天都在做这个。 I've researched it and the docs are a bit scarce.
我已经研究过了,文档有点稀缺。 I do recognize that I am getting the first ID at the top of the table because the IDs are all the same.
我确实认识到我在表格顶部获得了第一个 ID,因为这些 ID 都是相同的。 I don't know how to get the "right" ID when I'm deleting the row.
删除行时,我不知道如何获得“正确”的 ID。 The modal is popping..and the delete works...it's just the wrong record gets deleted...
模式正在弹出......并且删除有效......只是错误的记录被删除......
HTML HTML
{% for notify in notify_list %}
<tr style="vertical-align:top">
<td class="hide">
<div id="myModaldelete" class="modaldelete">
<div class="modal-content-delete">
<span class="closedelete"></span>
<img class="logo4" src="/static/images/threecircles.svg">
<p>Delete Request?</p>
<button type="button" class="button165" id="yesBtndelete" value="{{ notify.id }}">Yes</button>
<button type="button" class="button160" id="noBtndelete">No</button>
</div>
</div>
<button type="button" class="button114">
<div class="txtalgn114"><h2 class="txtalgn114a">Delete</h2></div></button>
</td>
</tr>
{% endfor %}
My Javascript...我的Javascript...
// Get the modal
var modaldelete = document.getElementById("myModaldelete");
// Get the button that opens the modal
var btndelete = document.getElementById("myBtndelete");
// Get the button that opens the modal
var nobtndelete = document.getElementById("noBtndelete");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("closedelete")[0];
// When the user clicks on <span> (x), close the modal
span.onclick = function(e) {
e.preventDefault();
modaldelete.style.display = "none";
}
// When the user clicks on the No button, close the modal
nobtndelete.onclick = function(e) {
e.preventDefault();
modaldelete.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modaldelete ) {
modaldelete.style.display = "none";
}
}
$(document).on('click', '.button114', function(e) { // note the e, thats the event
modaldelete.style.display = "block";
});
$(document).on('click', '.button165', function(e) { // note the e, thats the event
var id = $(this).val();
document.location.href = "/url/" + id;
});
I've read about maybe adding the ID to the ID name so that it's unique as a possible solution...I just can't figure out how to get it to work...Thanks in advance for any thoughts....I'm not using BootStrap on purpose...Just for the record...我读过关于可能将 ID 添加到 ID 名称,这样它作为一个可能的解决方案是独一无二的……我只是不知道如何让它工作……提前感谢您的任何想法……我不是故意使用 BootStrap...只是为了记录...
You might just be opening the wrong modal, since you're opening the modal with:您可能只是打开了错误的模态,因为您是通过以下方式打开模态的:
modaldelete = document.getElementById("myModaldelete");
but wouldn't there be more than one of these, because you're looping through and creating the modals in the:但是不会有不止一个这样的,因为你正在循环并在以下位置创建模态:
{% for notify in notify_list %}
You'd probably want to do something like:你可能想做类似的事情:
{% set index = 0 %}
{% for notify in notify_list %}
<tr style="vertical-align:top">
<td class="hide">
<div id="myModaldelete{{index}}" class="modaldelete">
...
{% index++ %}
{% endfor %}
which would give the modals different ids, and then you'd need to reference which modal to open with the button like:这将为模态提供不同的 ID,然后您需要引用使用按钮打开的模态,例如:
<button type="button" class="button114" onclick="openModal({{index}})">
<div class="txtalgn114"><h2 class="txtalgn114a">Delete</h2></div></button>
and inside openModal():在 openModal() 里面:
function openModal(index) {
var modaldelete = document.getElementById("myModaldelete" + index);
modaldelete.style.display = "block";
}
Hopefully that works!希望这有效!
First of all, you are doing a mistake by giving id
to repeated HTML elements.首先,您将
id
赋予重复的 HTML 元素是错误的。 You can avoid such by giving that id
value as class
instead.您可以通过将该
id
值改为class
来避免这种情况。 Then, you can give your row ID in a data
attribute:然后,您可以在
data
属性中提供您的行 ID:
<button type="button" class="myBtndelete" data-id="{{ notify.id }}">Yes</button>
<button type="button" class="noBtndelete">No</button>
Now, you can get all your myBtndelete
buttons by using getElementByClassName
instead.现在,您可以改用
getElementByClassName
来获取所有myBtndelete
按钮。
Also, you can get the ID value on click using:此外,您可以使用以下方法获取点击时的 ID 值:
$(document).on('click', '.myBtndelete', function(e) {
var id = $(this).data('id')
document.location.href = "/url/" + id;
});
Another way: You can replace your button
elements with a
elements in order to give them href
attribute on the fly without having to handle the click explicitly using JavaScript, but you have to handle the styling of the anchor a
elements to have the button look and feel like giving them some Bootstrap classes:另一种方法:您可以用
a
元素替换button
元素,以便动态地为它们提供href
属性,而不必使用 JavaScript 显式处理点击,但是您必须处理锚a
元素的样式以使按钮看起来和想给他们一些 Bootstrap 类:
<a href="/some-url/{{notify.id}}" class="btn btn-primary myBtndelete">Yes</a>
Ok...After about a day or two of playing with this concept...I figured it out...I was missing a few things...Most importantly the back and forth with the Javascript and the ID value...I had to add the following Javascript logic...I moved the modal out of my loop and added a hidden input to get the ID value.好吧......在玩这个概念大约一两天之后......我想通了......我遗漏了一些东西......最重要的是来回与 Javascript 和 ID 值......我必须添加以下 Javascript 逻辑...我将模态移出循环并添加隐藏输入以获取 ID 值。 Thanks to all who helped me piece this one together.
感谢所有帮助我拼凑这个的人。 It was complicated.
这很复杂。 For me anyway.
反正对我来说。
$('.button114').on('click', function (event) {
modaldelete[0].style.display = "block";
var id = $(this).data('id');
document.getElementById("modal-input-value").value = id;
});
$('.button165').on('click', function (event) {
// Get the hidden input element
var hiddenInput = document.getElementById("modal-input-value");
// Get the value of the hidden input
var id = hiddenInput.value;
document.location.href = "/url/" + id;
});
<div id="myModaldelete{{ notify.id }}" class="modaldelete">
<div class="modal-content-delete">
<span class="closedelete"></span>
<img class="logo4" src="/static/images/threecircles.svg">
<p>Delete Request?</p>
<input type="hidden" id="modal-input-value">
<button type="button" class="button165">Yes</button>
<button type="button" class="button160" id="noBtndelete">No</button>
</div>
</div>
{% for notify in notify_list %}
<tr style="vertical-align:top">
{% if user.userprofile.eDirector_queue_delete_confirm == "Yes" %}
<td class="hide">
<button type="button" class="button114" data-id="{{ notify.id }}">
<div class="txtalgn114"><h2 class="txtalgn114a">Delete</h2></div></button>
</td>
{% endif &}
{% endfor %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.