以下是我的代码:
function ConfirmYesNo(title, msg) {
var $confirm = $("#myModal");
$confirm.modal('show');
$("#myModalLabel").html(title);
$("#myModalText").html(msg);
$("#btnYes").off('click').click(function () {
$confirm.modal("hide");
return 1;
});
$("#btnNo").off('click').click(function () {
$confirm.modal("hide");
return 0;
});
}
function ValidationWarning(WarningMsg) {
var a = ConfirmYesNo(Warning", WarningMsg);
}
我想知道用户在继续之前是否按了是或否按钮。使用return 1
或return 0
对我不起作用。有什么想法吗?
答案 0 :(得分:5)
在jquery中尝试Promise:Jquery Promise 或deferred.promise()
function ConfirmYesNo(title, msg) {
var dfd = jQuery.Deferred();
var $confirm = $('#myModal');
$confirm.modal('show');
$('#myModalLabel').html(title);
$('#myModalText').html(msg);
$('#name').off('click').click(function () {
$confirm.modal('hide');
dfd.resolve(1);
return 1;
});
$('#btnNo').off('click').click(function () {
$confirm.modal('hide');
return 0;
});
return dfd.promise();
}
function ValidationWarning(WarningMsg) {
var a = ConfirmYesNo('dddd', ' WarningMsg');
a.then(function (b) {
console.log(b);
alert(b)
})
}
ValidationWarning()

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;