当用户访问我的网站时,我如何创建一个包含接受和拒绝两个选项的对话框?我需要接受按钮,以继续我的网站,并拒绝按钮,把访问者带到他们的最后一个网页。
到目前为止,我有:
var confirmation = confirm('Do you want to continue on this website?');
if (!confirmation){
// Redirect the user to his last webpage:
history.go(-1);
}发布于 2013-12-30 04:31:40
如果您想要使用jquery,则需要在页面中包含Jquery并定义以下函数
function confirmation(question) {
var defer = $.Deferred();
$('<div></div>')
.html(question)
.dialog({
autoOpen: true,
modal: true,
title: 'Confirmation',
buttons: {
"Accept": function () {
defer.resolve("true");//this text 'true' can be anything. But for this usage, it should be true or false.
$(this).dialog("close");
},
"Deny": function () {
defer.resolve("false");//this text 'false' can be anything. But for this usage, it should be true or false.
$(this).dialog("close");
}
}
});
return defer.promise();
};然后按照下面的方式使用它
confirmation('Do you want to continue on this website?').then(function (answer) {
if (answer == "false") {
// your logic
}
else if (answer == "true") {
// your logic
}
});发布于 2013-12-30 05:22:20
如果你想使用jQuery UI,你可以这样做:
HTML:
<div id="ask">
<h1> Some title </h1>
<p> Some info </p>
</div> JS:
$(function() {
$( "#ask" ).dialog({
resizable: false,
modal: true,
buttons: {
"Allow": function() {
alert("You have been allowed");
// more allow code here
},
"Deny": function() {
alert("You have been denied");
history.go(-1);
// more deny code here
}
}
});
}); FIDDLE
你必须在你的页面中包含jQuery UI。
https://stackoverflow.com/questions/20829701
复制相似问题