Skip to content

Instantly share code, notes, and snippets.

@arukusays
Last active November 4, 2020 15:36
Show Gist options
  • Save arukusays/db17c259d47cc62980f12ce53ad381a0 to your computer and use it in GitHub Desktop.
Save arukusays/db17c259d47cc62980f12ce53ad381a0 to your computer and use it in GitHub Desktop.
BootstrapのModalを使ったconfirmサンプル
$('#btnModal').on('click', async function(e){
if(await confirm('submitする??')){
console.log('submit!!');
} else {
console.log('cancel!!');
e.preventDefault();
e.stopPropagation();
}
});
function confirm(content){
return new Promise((resolve) => {
$('div.modal-body').text(content);
// 1回だけ実行
$('#btnModalOk').one('click', () => {
resolve(true);
});
$('#myModal').one('hidden.bs.modal', () => {
// OKボタンで先にresolveされていたら、そちらが優先.
resolve(false);
});
$('#myModal').modal('show');
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment