jQuery 带有警报 "OK" 按钮的事件被点击
jQuery event with Alert "OK" button clicked
我有一个 jQuery
函数,它在单击时触发警报弹出窗口。当按下确定按钮时,我需要触发另一个事件。在这种情况下,用户同意某事,我需要将他们同意的变量存储到本地存储。我当前的警报代码:
$('.external').click( function() { alert("You are now leaving site"); });
您正在寻找 confirm()
。试试这个:
$('.external').click(function (e) {
var r = confirm("You are now leaving site");
if (r == true) {
// user agreed, do something
} else {
// user did not agree, do something else
}
e.preventDefault();
});
编辑 1:
根据@JohnCarpenter,关于localStorage
:Storing Objects in HTML5 localStorage
编辑 2:
要在确认后重定向页面,请使用如下内容:
window.location.replace("http://domain.com");
您也可以喜欢 jQuery 并从原始锚点的 href
属性动态获取 URL:
window.location.replace($(this).prop('href'));
请参阅 this post 关于 window.location.replace
。 window.location.href
可能更适合您的需求。
我有一个 jQuery
函数,它在单击时触发警报弹出窗口。当按下确定按钮时,我需要触发另一个事件。在这种情况下,用户同意某事,我需要将他们同意的变量存储到本地存储。我当前的警报代码:
$('.external').click( function() { alert("You are now leaving site"); });
您正在寻找 confirm()
。试试这个:
$('.external').click(function (e) {
var r = confirm("You are now leaving site");
if (r == true) {
// user agreed, do something
} else {
// user did not agree, do something else
}
e.preventDefault();
});
编辑 1:
根据@JohnCarpenter,关于localStorage
:Storing Objects in HTML5 localStorage
编辑 2:
要在确认后重定向页面,请使用如下内容:
window.location.replace("http://domain.com");
您也可以喜欢 jQuery 并从原始锚点的 href
属性动态获取 URL:
window.location.replace($(this).prop('href'));
请参阅 this post 关于 window.location.replace
。 window.location.href
可能更适合您的需求。