Javascript 默认操作未在新呼叫时重置
我对JavaScript中的preventdefault有一些问题,特别是jQuery。我正在尝试获取一个模态框,以显示具有类的元素。确认如果用户确定要执行该操作,那么在继续执行该操作之前,该类元素将有效覆盖默认操作确认Javascript 默认操作未在新呼叫时重置,javascript,jquery,preventdefault,Javascript,Jquery,Preventdefault,我对JavaScript中的preventdefault有一些问题,特别是jQuery。我正在尝试获取一个模态框,以显示具有类的元素。确认如果用户确定要执行该操作,那么在继续执行该操作之前,该类元素将有效覆盖默认操作确认 $('.confirm').on('click', function(event, options) { options = options || {}; if (!options.confirmed) { vex.dialog.con
$('.confirm').on('click', function(event, options)
{
options = options || {};
if (!options.confirmed)
{
vex.dialog.confirm({
message: 'Are you sure that you want to perform this action?',
callback: function(response) {
if (response === true) {
$(event.target).trigger('click', {confirmed: response});
}
}
});
event.preventDefault();
}
});
流
显示一个模态对话框。
如果用户确认该操作,则再次触发click事件,只有这一次确认后才会传递回事件处理程序。
这将跳过检查并继续执行默认操作。
问题
但事实并非如此,因为不管发生什么,默认操作都会覆盖任何未来调用
尝试
取消/重新绑定-需要用户再次单击它才能工作。
window.location不理想,因为它假定操作是链接,但可以工作
confirm与美学不匹配,因此需要更好的解决方案
笔记
需要是异步的。
任何帮助都将是非常棒的,因为它让我暂时感到困惑在API调用之前放置event.preventDefault,我想这应该可以工作
$('.confirm').on('click', function(event, options)
{
options = options || {};
if (!options.confirmed)
{
event.preventDefault();
vex.dialog.confirm({
message: 'Are you sure that you want to perform this action?',
callback: function(response) {
if (response === true) {
$(event.target).trigger('click', {confirmed: response});
}
}
});
}
});
编辑:
我想这是你说的唯一不解除绑定/重新绑定的方法
$('.confirm').on('click', function(event, options)
{
options = options || {};
if (!options.confirmed)
{
event.preventDefault();
vex.dialog.confirm({
message: 'Are you sure that you want to perform this action?',
callback: function(response) {
if (response === true) {
$(event.target).trigger('click', {confirmed: response});
}
}
});
}
else {
window.location = $(this).attr("href");
}
});
请尝试在回调中删除带有$'.confirm'.off的单击处理程序。这样,当您以编程方式触发单击时,将调用单击处理程序。使用ifconfirma是否确定?{alert'yes';};由于不需要防止默认,它需要异步,因为我正在使用这个使用回调的模式库。我认为这是你需要使用的,而不是preventDefault@TheManiac谢谢,但这并不能阻止默认操作。是的,我现在明白了。实际上,唯一可能的是vex库通过点击事件做了一些时髦的事情。我使用console.log设置了这个fiddle,一切正常:不,这似乎也不起作用,并不是说移动它真的会有很大的不同,因为对话框是异步的,所以应该可以通过。对不起,我第一次误解了你的问题。什么是元素,确认吗。您确定第二个单击事件正在触发吗?第二个事件正在正确触发,但由于最初的尝试被阻止,似乎阻止了预期的操作。在本例中,链接被触发。哦,我知道问题。。。你最好的选择是“如果”!options.confirmed{//code}else{window.location=$this.attrref;}可以工作,但需要像以前的解决方案一样再次单击该按钮以重新/解除绑定。谢谢,尽管我们正在慢慢靠近: