【问题标题】:Create confirmation box with "accept" and "deny"创建带有“接受”和“拒绝”的确认框
【发布时间】:2013-12-29 20:24:14
【问题描述】:

当用户访问我的网站时,我如何制作一个包含两个选项(接受和拒绝)的对话框?我需要接受按钮才能继续访问我的网站,需要拒绝按钮才能将访问者带到他们的最后一个网页。

到目前为止我有:

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

【问题讨论】:

标签: javascript modal-dialog


【解决方案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();
};

然后按照以下方式使用它

 confirmation('Do you want to continue on this website?').then(function (answer) {

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

                }
                else if (answer == "true") {
                    // your logic
                }
            });

【讨论】:

    【解决方案2】:

    如果你想使用 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

    【讨论】:

      猜你喜欢
      • 2020-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-11
      • 2011-11-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-18
      相关资源
      最近更新 更多