[英](jQuery) How to avoid registering multiple click events under another click event?
I have 2 click events under one click event for a Delete jQuery dialog popup.对于删除 jQuery 对话框弹出窗口,我在单击事件下有 2 个单击事件。 What it is doing right now that when I click delete a Course, it opens up a dialog popup, and I can either click "Yes" to confirm deletion, or "No" to close the dialog box.它现在正在做什么,当我单击删除课程时,它会打开一个对话框弹出窗口,我可以单击“是”确认删除,或单击“否”关闭对话框。
It is working, but when I log the "No" button and click it twice, the Developer Tools Console shows I have clicked it 3 times.它正在工作,但是当我记录“否”按钮并单击它两次时,开发人员工具控制台显示我已单击它 3 次。 Like each click after the first one, the console doubles it.就像第一次点击之后的每一次点击一样,控制台将它加倍。 I am not being able to work out on how to separate the click events and still work inside of the same dialog popup box?我无法解决如何分离点击事件并仍然在同一个对话框弹出框内工作?
<div class="table-content">
..........
@Html.ActionLink("Delete", "Delete", new { id = item.CourseID }, new { @class = "deletebtn", @id = item.CourseID })
</div>
<div id="dialog" title="Delete Department" style="display: none">
<p>Are you sure you want to delete this Course?</p>
<button id="confirm-deletion">Yes</button>
<button id="abort-deletion">No</button>
</div>
<script>
$(function () {
$(".deletebtn").on("click", function (e) {
e.preventDefault();
$('#dialog').dialog();
var btnobj = $(this);
var id = btnobj[0].id;
$('#confirm-deletion').on("click", function () {
var token = $('input[name="__RequestVerificationToken"]').val();
var data = { id: id, __RequestVerificationToken: token };
$.ajax({
type: "POST",
url: "@Url.Action("Delete","Course")",
data: data,
//ajaxasync: true,
success: function () {
$("#dialog").dialog("close");
$('div.table-content').empty().load(' .table-content');
//console.log("success");
},
error: function () {
console.log("failed");
}
});
});
$('#abort-deletion').on("click", function () {
console.log("NoClick");
$("#dialog").dialog("close");
});
});
});
</script>
Don't create a click event inside of a click event. 不要点击事件中创建一个click事件。 This will, as you've found, add a new event with every click. 如您所见,每次点击都会添加一个新事件。 Create it once when the page loads: 页面加载后,只需创建一次即可 :
$(function () {
$(".deletebtn").on("click", function (e) {
e.preventDefault();
$('#dialog').dialog();
var btnobj = $(this);
var id = btnobj[0].id;
// don't create click events here
});
$('#confirm-deletion').on("click", function () {
//...
});
$('#abort-deletion').on("click", function () {
//...
});
});
If the problem you encounter is that the elements aren't yet defined when the page loads, you can bind the event to a common parent and use event delegation : 如果您遇到的问题是页面加载时尚未定义元素,则可以将事件绑定到公共父对象并使用事件委托 :
$(document).on("click", '#confirm-deletion', function () {
//...
});
let clickRegistered = false;
$(".deletebtn").on("click", function (e) {
if(!clickRegistered){
clickRegistered = true;
//... registerComponents/call functions ...
}
});
clickRegistered
is used to make sure that function body wil execute only once. clickRegistered
用于确保 function 主体仅执行一次。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.