Jquery 使用Bootstrap 3模式框确认删除
我需要使用Bootstrap 3模式框(是/否)确认删除。我如何创建这个 HTML代码:Jquery 使用Bootstrap 3模式框确认删除,jquery,css,twitter-bootstrap-3,bootstrap-modal,confirm,Jquery,Css,Twitter Bootstrap 3,Bootstrap Modal,Confirm,我需要使用Bootstrap 3模式框(是/否)确认删除。我如何创建这个 HTML代码: <form action="blah" method="POST"> <button class='btn' type="submit" name="remove_levels" value="delete"> <span class="fa fa-times"></span> Delete </button> &l
<form action="blah" method="POST">
<button class='btn' type="submit" name="remove_levels" value="delete">
<span class="fa fa-times"></span> Delete
</button>
</form>
删除
对于您的按钮:
<button class='btn btn-danger btn-xs launchConfirm' type="button" name="remove_levels"><span class="fa fa-times"></span> delete</button></td>
删除
您可以使用对话框
$(document).ready(function() {
$('#btnDelete').click(function() {
bootbox.confirm("Are you sure want to delete?", function(result) {
alert("Confirm result: " + result);
});
});
});
您需要在HTML中使用模态。单击“删除”按钮时,会弹出模式。防止点击该按钮提交表单也很重要。单击确认后,表单将提交
$('button[name=“remove_levels”]”)。在('click',函数(e){
var$form=$(this).closest('form');
e、 预防默认值();
$('#确认')。模态({
背景:“静态”,
键盘:错误
})
.on('单击','删除'),函数(e){
$form.trigger('submit');
});
$(“#取消”)。在('click',函数(e){
e、 预防默认值();
$('#confirm').modal.model('hide');
});
});代码>
删除
你确定吗?
删除
取消
使用情态动词的简单方法是
出票人:
链接到eModal.js
var选项={
信息:“著名的问题?”,
标题:“标题标题”,
尺寸:'sm',
回调:函数(结果){result?doActionTrue(结果):doActionFalse();},
副标题:“较小的文本标题”,
标签:“真”//使用可能标签作为键
//...
};
eModal.confirm(选项)代码>
使用id=“confirmation”在HTML中创建模式对话框,并使用函数showConfirmation
还请记住,在隐藏模式对话框之后,应该取消绑定(modal.unbind())取消和成功按钮。如果你不这样做,你会得到双重绑定。
例如:如果您打开对话框一次并按“取消”,然后在第二次打开对话框并按“确定”,您将得到两次成功回调的执行
showConfirmation = function(title, message, success, cancel) {
title = title ? title : 'Are you sure?';
var modal = $("#confirmation");
modal.find(".modal-title").html(title).end()
.find(".modal-body").html(message).end()
.modal({ backdrop: 'static', keyboard: false })
.on('hidden.bs.modal', function () {
modal.unbind();
});
if (success) {
modal.one('click', '.modal-footer .btn-primary', success);
}
if (cancel) {
modal.one('click', '.modal-header .close, .modal-footer .btn-default', cancel);
}
};
// bind confirmation dialog on delete buttons
$(document).on("click", ".delete-event, .delete-all-event", function(event){
event.preventDefault();
var self = $(this);
var url = $(this).data('url');
var success = function(){
alert('window.location.href=url');
}
var cancel = function(){
alert('Cancel');
};
if (self.data('confirmation')) {
var title = self.data('confirmation-title') ? self.data('confirmation-title') : undefined;
var message = self.data('confirmation');
showConfirmation(title, message, success, cancel);
} else {
success();
}
});
就在今天,我也遇到了同样的问题。这是我的解决方案(我认为更好更简单):
在one
之前使用unbind
可防止在下次打开对话框时调用删除功能
我希望这能有所帮助
下面是一个完整的例子:
var YOUR_MESSAGE_STRING_CONST=“YOUR confirm MESSAGE?”;
$('btnDelete')。在('click',函数(e){
confirmDialog(您的消息字符串常量,函数(){
//我要删除的代码
console.log(“已删除!”);
});
});
函数确认对话框(消息,onConfirm){
var fClose=函数(){
模态。模态(“隐藏”);
};
var模式=$(“确认模式”);
模态。模态(“显示”);
$(“#confirmMessage”).empty().append(message);
$(“#confirmOk”).unbind().one('click',onConfirm.).one('click',fClose);
$(“确认取消”).unbind().one(“单击”,fClose);
}
删除
好啊
取消
以下解决方案优于bootbox.js,因为
- 它可以做bootbox.js所能做的一切李>
- 使用语法更简单
- 它允许您使用“错误”、“警告”或“信息”优雅地控制邮件的颜色
- 行李箱长986行,我的只有110行
digimango.messagebox.js:
const dialogTemplate='2〕\
\
\
\
\
模式中的一些文本。
\
\
\
\
嗯\
取消\
\
\
\
';
//请参阅函数digimango_onOkClick(事件)中的注释{
var digimango_NumofDialogSopend=0;
功能消息框(消息、意义、选项、操作确认回调){
if($('#digimango_MessageBoxContainer')。长度==0){
var iDiv=document.createElement('div');
iDiv.id='digimango_MessageBoxContainer';
document.getElementsByTagName('body')[0].appendChild(iDiv);
$(“#digimango_MessageBoxContainer”).html(dialogTemplate);
}
变量okButtonName、cancelButtonName、showTextBox、textBoxDefaultText;
如果(选项==null){
okButtonName=‘OK’;
cancelButtonName=null;
showTextBox=null;
textBoxDefaultText=null;
}否则{
okButtonName=options.okButtonName;
cancelButtonName=options.cancelButtonName;
showTextBox=options.showTextBox;
textBoxDefaultText=options.textBoxDefaultText;
}
如果(showTextBox==true){
如果(textBoxDefaultText==null)
$('#digimango_messageBoxTextArea').val('';
其他的
$('#digimango_messageBoxTextArea').val(textBoxDefaultText);
$('#digimango_messageBoxTextArea').show();
}
其他的
$('#digimango_messageBoxTextArea').hide();
如果(okButtonName!=null)
$('#digimango_messageBoxOkButton').html(okButtonName);
其他的
$('#digimango_messageBoxOkButton').html('OK');
if(cancelButtonName==null)
$(“#digimango_messageBoxCancelButton”).hide();
否则{
$(“#digimango_messageBoxCancelButton”).show();
$('#digimango_messageBoxCancelButton').html(cancelButtonName);
}
$(“#digimango_messageBoxOkButton”)。解除绑定('click');
$(“#digimango_messageBoxOkButton”)。在('click',{callback:actionConfirmedCallback},digimango_onOkClick)上;
$('digimango'u messageBoxCancelButton')。解除绑定('click');
$('digimango'u messageBoxCancelButton')。在('click',digimango'u onCancelClick');
var content=$(“#digimango_messageBoxMessage”);
如果(显著性=‘错误’)
content.attr('class','text danger');
否则,如果(重要性==‘警告’)
康顿
showConfirmation = function(title, message, success, cancel) {
title = title ? title : 'Are you sure?';
var modal = $("#confirmation");
modal.find(".modal-title").html(title).end()
.find(".modal-body").html(message).end()
.modal({ backdrop: 'static', keyboard: false })
.on('hidden.bs.modal', function () {
modal.unbind();
});
if (success) {
modal.one('click', '.modal-footer .btn-primary', success);
}
if (cancel) {
modal.one('click', '.modal-header .close, .modal-footer .btn-default', cancel);
}
};
// bind confirmation dialog on delete buttons
$(document).on("click", ".delete-event, .delete-all-event", function(event){
event.preventDefault();
var self = $(this);
var url = $(this).data('url');
var success = function(){
alert('window.location.href=url');
}
var cancel = function(){
alert('Cancel');
};
if (self.data('confirmation')) {
var title = self.data('confirmation-title') ? self.data('confirmation-title') : undefined;
var message = self.data('confirmation');
showConfirmation(title, message, success, cancel);
} else {
success();
}
});
<!-- Modal dialog -->
<div class="modal fade" id="frmPrenotazione" tabindex="-1">
<!-- CUTTED -->
<div id="step1" class="modal-footer">
<button type="button" class="glyphicon glyphicon-erase btn btn-default" id="btnDelete"> Delete</button>
</div>
</div>
<!-- Modal confirm -->
<div class="modal" id="confirmModal" style="display: none; z-index: 1050;">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body" id="confirmMessage">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" id="confirmOk">Ok</button>
<button type="button" class="btn btn-default" id="confirmCancel">Cancel</button>
</div>
</div>
</div>
</div>
$('#btnDelete').on('click', function(e){
confirmDialog(YOUR_MESSAGE_STRING_CONST, function(){
//My code to delete
});
});
function confirmDialog(message, onConfirm){
var fClose = function(){
modal.modal("hide");
};
var modal = $("#confirmModal");
modal.modal("show");
$("#confirmMessage").empty().append(message);
$("#confirmOk").unbind().one('click', onConfirm).one('click', fClose);
$("#confirmCancel").unbind().one("click", fClose);
}
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
Launch demo modal
</button>
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>