Javascript 模式引导用户确认
如何创建一个JQuery函数,等待用户确认是否真的要删除数据 我如何才能实现这样的目标:Javascript 模式引导用户确认,javascript,jquery,asp.net-mvc,twitter-bootstrap,bootstrap-modal,Javascript,Jquery,Asp.net Mvc,Twitter Bootstrap,Bootstrap Modal,如何创建一个JQuery函数,等待用户确认是否真的要删除数据 我如何才能实现这样的目标: function UserConfirmation() { ShowModal(); //"Global" Delete Confirmation Bootstrap Modal //Now wait till the user decides whether or not he really wants to //delete data(Button events or on Moda
function UserConfirmation()
{
ShowModal(); //"Global" Delete Confirmation Bootstrap Modal
//Now wait till the user decides whether or not he really wants to
//delete data(Button events or on Modal hide).
//On user reply:
if(UserReplied == 'Delete')
{}
else
{}
}
编辑:
这是布局中模式的html代码。无论何时需要,都可以通过showmodel()函数调用此ModalDelete
<!-- Delete Data Confirmation Modal -->
<div class="modal fade" id="ModalDelete" role="dialog" data-backdrop="static" data-keyboard="false">
<div class="modal-dialog modal-sm">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" id="btn-cancel" data-dismiss="modal">×</button>
<h4 class="modal-title">Confirm Delete</h4>
</div>
<div class="modal-body">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-success" id="btn-cancel" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-danger btn-delete" id="btn-delete" data-dismiss="modal">Delete</button>
</div>
</div>
</div>
</div>
&时代;
确认删除
取消
删除
函数UserConfirmation在调用showmodel()后侦听模式事件,并根据用户的选择继续按下按钮或模式隐藏
我认为这可以通过承诺或不同的行动来实现 当您需要删除确认时,您可以使用例如,然后类似以下内容:
BootstrapDialog.show({
title: 'Alert',
message: 'Are you sure you want to delete this ?',
buttons: [{
label: 'Yes',
cssClass: 'btn-danger',
action: function(dialog) {
dialog.close();
//your action
}
}, {
label: 'No',
action: function(dialog) {
dialog.close();
}
}]
});`
好的,下面是我如何使用promise和Jquery解决这个问题的:
function ModalOptionChoosen() {
var deferred = $.Deferred();
$('#ModalDelete button').on('click', function (e) {
var $target = $(e.target); // Clicked button element
$(this).closest('.modal').on('hidden.bs.modal', function () {
//console.log($target); // Logged if the clicked button actually closed the modal
var result = $target.attr('id');
deferred.resolve(result);
});
});
return deferred.promise();
};
function ShowModalWaitForResult () {
ShowModal();
var promise = ModalOptionChoosen();
promise.done(function (result) {
console.log(result); // log the user selected value
});
});
现在,如果需要,我可以在任何地方引用ModalOptionChoosen函数。数据存储在哪里?布局中设计了模态html。如果这是您要问的。从何处获取
userrepled
的值。你能提供一个提琴,这只是一个例子,我正在努力实现。顺便说一句,我想知道为什么我得到了落选?!!