Javascript 用“创建确认框”;接受;及;“否认”;
当用户访问我的网站时,我如何创建包含两个选项(接受和拒绝)的对话框?我需要“接受”按钮继续访问我的网站,需要“拒绝”按钮将访问者带到他们的最后一个网页 到目前为止,我已经:Javascript 用“创建确认框”;接受;及;“否认”;,javascript,modal-dialog,Javascript,Modal Dialog,当用户访问我的网站时,我如何创建包含两个选项(接受和拒绝)的对话框?我需要“接受”按钮继续访问我的网站,需要“拒绝”按钮将访问者带到他们的最后一个网页 到目前为止,我已经: var confirmation = confirm('Do you want to continue on this website?'); if (!confirmation){ // Redirect the user to his last webpage: history.go(-1); } 如果
var confirmation = confirm('Do you want to continue on this website?');
if (!confirmation){
// Redirect the user to his last webpage:
history.go(-1);
}
如果您想使用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();
};
如果您想使用jQuery UI,可以这样做: HTML:
您必须在页面中包含。您必须使用第三方库,如bootstrap或jqueryui。许多框架提供可定制的弹出窗口。使用其中一种是一种选择吗?(@DanielA.White-您不必使用第三方库;如果您使用,会方便得多。),
confirmation('Do you want to continue on this website?').then(function (answer) {
if (answer == "false") {
// your logic
}
else if (answer == "true") {
// your logic
}
});
<div id="ask">
<h1> Some title </h1>
<p> Some info </p>
</div>
$(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
}
}
});
});