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)
Jos*_*ter 17
您应该看看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)
| 归档时间: |
|
| 查看次数: |
99930 次 |
| 最近记录: |