【发布时间】:2017-03-08 20:23:31
【问题描述】:
所以我有一个正在开发的网络应用程序,并且我有一个应用程序部分有一个部署按钮,它启动一个模式。在模式中,我还有两个按钮,一个用于确认,一个用于取消。
当模式启动并且我单击其中一个按钮时,它会触发一次。但是,我的问题是,如果我退出模式,重新打开它并再次单击一个按钮,它将触发该按钮两次。如果我重复这个过程,这种情况会继续,所以下一次点击会触发三次点击,依此类推。
我尝试在 jQuery 中使用 unbind 和 one 无济于事,此时我有点难过。
这是该部分的相关 jQuery。如果有帮助,我还可以附上我正在使用的车把文件。
jQuery:
//open modal, present OK and Cancel buttons
$(".inventory").on('click', 'button.deploy', function () {
console.log('DEPLOY')
var machine = $(this).data("machine");
var message = "Are you <b>SURE</b> you want to deploy the following machine:\n\n<b>" + $(this).data("machine") + "</b>";
var newNetwork = $(this).parent().siblings(".select-option").find("option:selected").val();
var currentDefault = $(this).parent().siblings(".select-option").attr('defaultValue');
if (currentDefault !== newNetwork) {
message += "\n\nChanging the network from: \n\n " + currentDefault + " -> <b>" + newNetwork + "</b>\n\n";
}
else {
message += "\n\nRestarting with the network:\n\n<b>" + currentDefault + "</b>\n\n";
}
message += "<i>NOTE: Any other network changes made to other machines will result in possible changes "
message += "the next time there is a full deploy.</i>"
$("#reset-message").html(message);
$(".jquery-modal.blocker.current").show();
$("div #reset-modal.modal").show();
$("div #reset-modal").on('click', "#deploy-cancel", function () {
$(".jquery-modal.blocker.current").hide();
$("div #reset-modal.modal").hide();
console.log('CANCEL')
});
$("div #reset-modal").on('click', "#deploy-confirm", function () {
$(".jquery-modal.blocker.current").hide();
$("div #reset-modal.modal").hide();
console.log('CONFIRM')
});
【问题讨论】:
-
将模式事件附加到 $(".inventory").on('click') 之外,或者在隐藏/关闭时销毁模式并在需要时重新创建它。目前,您似乎隐藏了模式并重用它,但每次显示它时都会附加一组新的点击处理程序。
-
所以我需要点击
button.deploy来触发cancel和confirm按钮的显示,以及打开模式。如果这些事件不包含在部署按钮的单击事件中,我该怎么做? -
@JonSG 你是救生员!刚刚想通了。非常感谢。
标签: javascript jquery events button handlebars.js