从bootstrap的模态框中返回一个值yes或no选项

时间:2015-04-10 03:36:54

标签: javascript jquery html css twitter-bootstrap

以下是我的代码:

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 1return 0对我不起作用。有什么想法吗?

1 个答案:

答案 0 :(得分:5)

在jquery中尝试Promise:Jquery Promisedeferred.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;
&#13;
&#13;