JavaScript中的自定义"确认"对话框?

Dan*_*don 29 javascript jquery dialog jquery-ui modal-dialog

我一直在研究一个使用自定义"模态对话框"的ASP.net项目.我在这里使用恐慌引用是因为我理解'模态对话框'只是我的html文档中的一个div,它被设置为显示在文档其余部分的"顶部",而不是真正意义上的模态对话框.

在网站的许多部分,我的代码如下所示:

var warning = 'Are you sure you want to do this?';
if (confirm(warning)) {
    // Do something
}
else {
    // Do something else
}
Run Code Online (Sandbox Code Playgroud)

这没关系,但是让确认对话框与页面其余部分的样式相匹配会很不错.

但是,由于它不是真正的模态对话框,我认为我需要写这样的东西:(在这个例子中我使用jQuery-UI)

<div id='modal_dialog'>
    <div class='title'>
    </div>
    <input type='button' value='yes' id='btnYes' />
    <input type='button' value='no' id='btnNo' />
</div>

<script>
function DoSomethingDangerous() {
    var warning = 'Are you sure you want to do this?';
    $('.title').html(warning);
    var dialog = $('#modal_dialog').dialog();
    function Yes() {
        dialog.dialog('close');
        // Do something
    }   
    function No() {
        dialog.dialog('close');
        // Do something else
    }    
    $('#btnYes').click(Yes);
    $('#btnNo').click(No);
}
Run Code Online (Sandbox Code Playgroud)

这是实现我想要的好方法,还是有更好的方法?

aln*_*h29 47

您可能需要考虑将其抽象为如下函数:

function dialog(message, yesCallback, noCallback) {
    $('.title').html(message);
    var dialog = $('#modal_dialog').dialog();

    $('#btnYes').click(function() {
        dialog.dialog('close');
        yesCallback();
    });
    $('#btnNo').click(function() {
        dialog.dialog('close');
        noCallback();
    });
}
Run Code Online (Sandbox Code Playgroud)

然后你可以像这样使用它:

dialog('Are you sure you want to do this?',
    function() {
        // Do something
    },
    function() {
        // Do something else
    }
);
Run Code Online (Sandbox Code Playgroud)

  • 在设置 `on click` 事件之前调用 `.off('click')` 是一个很好的做法,以确保您的对话框不会同时触发多个事件。 (4认同)
  • 但无论我怎么做,我都要定义自定义函数,对吧?没有办法能够用内置的`confirm`函数来编写`if(confirm('blah?'))` (3认同)
  • @AndrewBrown但是函数什么时候会返回?这才是重点.您正在等待用户点击某些内容.要在返回之前检测到这一点,你需要在函数体内执行,这只能通过旋转来完成,这是一个糟糕的主意. (3认同)
  • 如果你的自定义确认功能只是返回TRUE或FALSE?然后通过`if(customConfirm()){//做某事}其他{//做别的事情}`来处理它 (2认同)
  • 对于阅读本文的任何人来说,按照 @Ramtin 的建议进行操作非常重要,否则每次单击“是”按钮时,它都会触发多个事件。当然,我是通过艰难的方式才发现这一点的。 (2认同)

Jos*_*ter 17

SweetAlert

您应该看看SweetAlert作为保存一些工作的选项.它在默认状态下很漂亮,并且可以高度自定义.

确认示例

sweetAlert(
  {
    title: "Are you sure?",
    text: "You will not be able to recover this imaginary file!",
    type: "warning",   
    showCancelButton: true,   
    confirmButtonColor: "#DD6B55",
    confirmButtonText: "Yes, delete it!"
  }, 
  deleteIt()
);
Run Code Online (Sandbox Code Playgroud)

示例警报


use*_*707 11

为了使您能够像正常的确认对话框一样使用确认框,我将使用 Promises,这将使您能够等待结果的结果,然后对此采取行动,而不必使用回调。

这将允许您使用与代码其他部分相同的模式,例如...

  const confirm = await ui.confirm('Are you sure you want to do this?');

  if(confirm){
    alert('yes clicked');
  } else{
    alert('no clicked');
  }
Run Code Online (Sandbox Code Playgroud)

例如,请参阅 codepen,或运行下面的代码段。

https://codepen.io/larnott/pen/rNNQoNp

在此处输入图片说明

  const confirm = await ui.confirm('Are you sure you want to do this?');

  if(confirm){
    alert('yes clicked');
  } else{
    alert('no clicked');
  }
Run Code Online (Sandbox Code Playgroud)
const ui = {
  confirm: async (message) => createConfirm(message)
}

const createConfirm = (message) => {
  return new Promise((complete, failed)=>{
    $('#confirmMessage').text(message)

    $('#confirmYes').off('click');
    $('#confirmNo').off('click');
    
    $('#confirmYes').on('click', ()=> { $('.confirm').hide(); complete(true); });
    $('#confirmNo').on('click', ()=> { $('.confirm').hide(); complete(false); });
    
    $('.confirm').show();
  });
}
                     
const saveForm = async () => {
  const confirm = await ui.confirm('Are you sure you want to do this?');
  
  if(confirm){
    alert('yes clicked');
  } else{
    alert('no clicked');
  }
}
Run Code Online (Sandbox Code Playgroud)
body {
  margin: 0px;
  font-family: "Arial";
}

.example {
  padding: 20px;
}

input[type=button] {
  padding: 5px 10px;
  margin: 10px 5px;
  border-radius: 5px;
  cursor: pointer;
  background: #ddd;
  border: 1px solid #ccc;
}
input[type=button]:hover {
  background: #ccc;
}

.confirm {
  display: none;
}
.confirm > div:first-of-type {
  position: fixed;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  top: 0px;
  left: 0px;
}
.confirm > div:last-of-type {
  padding: 10px 20px;
  background: white;
  position: absolute;
  width: auto;
  height: auto;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  border-radius: 5px;
  border: 1px solid #333;
}
.confirm > div:last-of-type div:first-of-type {
  min-width: 150px;
  padding: 10px;
}
.confirm > div:last-of-type div:last-of-type {
  text-align: right;
}
Run Code Online (Sandbox Code Playgroud)