最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Return a value from bootstrap's modal box yes or no option - Stack Overflow

programmeradmin3浏览0评论

Below is my code:

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);
}

I want to know whether the user had pressed Yes or No button before proceeding. Using return 1 or return 0 does not work for me. Any ideas?

Below is my code:

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);
}

I want to know whether the user had pressed Yes or No button before proceeding. Using return 1 or return 0 does not work for me. Any ideas?

Share Improve this question edited Apr 10, 2015 at 3:59 Downgoat 14.4k7 gold badges47 silver badges72 bronze badges asked Apr 10, 2015 at 3:36 CoolguyCoolguy 2,28512 gold badges56 silver badges81 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

Try Promise in jquery: Jquery Promise or 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./ajax/libs/jquery/2.1.1/jquery.min.js"></script>

发布评论

评论列表(0)

  1. 暂无评论