这里是场景,我的内容是基于一个类异步加载的。所以,如果我有这个类的链接ajaxLink它触发如下:防止多个点击事件触发JQuery
$('a.ajaxLink').click(function (e) {
e.preventDefault();
var container = $(this).parents('div.fullBottomContent');
var href = $(this).attr('href');
container.fadeOut('fast', function() {
$.ajax({
url: href,
dataType: "html",
type: "GET",
success: function (data) {
container.html(data);
BindEventHandlers();
container.fadeIn();
$.validator.unobtrusive.parse('form');
},
error: function() {
alert('An error has occurred');
}
});
});
});
所有可爱。现在,在一种情况下我想,所以我已经写了显示一个警告用户confirm
,他们要加载的页面并失去其所有的变化是:
$('a.addANewHotel').click(function (e) {
if (!confirm('Adding a new hotel will loose any unsaved changes, continue?')) {
e.stopPropagation();
}
});
现在我已经试过return false
,e.preventDefault()
和e.stopPropagation();
但无论第一种方法总是被解雇?我怎样才能防止额外的点击事件发射?这是事件顺序吗?
看不出这是相关的,但我的HTML是:
<a style="" href="/CMS/CreateANewHotel?regionID=3&destinationID=1&countryName=Australia" class="button wideBorderlessButton ajaxLink addANewHotel">Add a new hotel</a>
什么是你的HTML是什么样子? – Asciiom
我不认为HTML有任何阻碍,但无论如何加入 – Liam
我不明白你为什么要通过绑定2个不同的事件到一个相同的元素。我只是显示/不显示确认消息,取决于哪个元素被点击,但所有内部相同的点击事件(例如使用hasClass if) – Th0rndike