首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用"accept“和"deny”创建确认框

使用"accept“和"deny”创建确认框
EN

Stack Overflow用户
提问于 2013-12-30 04:24:15
回答 2查看 2.4K关注 0票数 1

当用户访问我的网站时,我如何创建一个包含接受和拒绝两个选项的对话框?我需要接受按钮,以继续我的网站,并拒绝按钮,把访问者带到他们的最后一个网页。

到目前为止,我有:

代码语言:javascript
复制
var confirmation = confirm('Do you want to continue on this website?');
if (!confirmation){
    // Redirect the user to his last webpage:
    history.go(-1);
}
EN

回答 2

Stack Overflow用户

发布于 2013-12-30 04:31:40

如果您想要使用jquery,则需要在页面中包含Jquery并定义以下函数

代码语言:javascript
复制
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();
};

然后按照下面的方式使用它

代码语言:javascript
复制
 confirmation('Do you want to continue on this website?').then(function (answer) {

                if (answer == "false") {
                    // your logic

                }
                else if (answer == "true") {
                    // your logic
                }
            });
票数 1
EN

Stack Overflow用户

发布于 2013-12-30 05:22:20

如果你想使用jQuery UI,你可以这样做:

HTML:

代码语言:javascript
复制
<div id="ask">
    <h1> Some title </h1>
    <p> Some info </p>
</div>  

JS:

代码语言:javascript
复制
  $(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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20829701

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档