使用“接受”和“拒绝”创建确认框

时间:2013-12-29 20:24:15

标签: 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);
}

2 个答案:

答案 0 :(得分: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
                }
            });

答案 1 :(得分:0)

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