Bootstrap模式 - 如果单击是按钮,如何返回True - 自定义确认

时间:2017-08-03 09:34:54

标签: javascript jquery bootstrap-modal

好的,我花了一整天时间阅读Q& A但仍然无法完成。我需要创建一个自定义的confirm(),而不是默认对话框,如果用户单击是,则使用bootstrap模式返回true,否则返回false。

示例:

<a href="remove.php?id=3" onclick="return custom_confirm('Are you sure you want to remove this?');" />

我的自定义确认功能是:

function custom_confirm(message) {
  //  put message into the body of modal
  $('#modal-custom-confirmation').on('show.bs.modal', function (event) {
  //  store current modal reference
    var modal = $(this);

    //  update modal body help text
    modal.find('.modal-body #modal-help-text').text(message);
  });

  //  show modal ringer custom confirmation
  $('#modal-custom-confirmation').modal('show');

  //  if Yes button is clicked, return true
  //  if No  button is clicked,  return false
  //  logic here...
}

我的模态在这里:

<!--  modal: custom confirmation  -->
<div class="modal fade text-left" id="modal-custom-confirmation" tabindex="-1" role="dialog" aria-labelledby="modal-help-title">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <strong class="modal-title" id="modal-help-title">Confirm</strong>
      </div><!--  /.modal-header  -->
      <div class="modal-body">
        <p id="modal-help-text"></p>
      </div><!--  /.modal-body  -->
      <div class="modal-footer">
        <button type="button" class="btn btn-success">Yes</button>
        <button type="button" class="btn btn-default" data-dismiss="modal">No</button>
      </div><!--  /.modal-footer  -->
    </div><!--  /.modal-content  -->
  </div><!--  /.modal-dialog  -->
</div><!--  /.modal  -->

2 个答案:

答案 0 :(得分:3)

Click事件本身就是非阻塞和异步。与原生confirm不同。因此,您无法返回truefalse。您必须以某种方式处理用户交互的异步性质。

最直接的方法是传递回调

function custom_confirm(message, callback) {
  //  put message into the body of modal
  $('#modal-custom-confirmation').on('show.bs.modal', function (event) {
  //  store current modal reference
    var modal = $(this);

    //  update modal body help text
    modal.find('.modal-body #modal-help-text').text(message);
  });

  //  show modal ringer custom confirmation
  $('#modal-custom-confirmation').modal('show');

  $('#modal-custom-confirmation button.ok').off().on('click', function() {
     // close window
     $('#modal-custom-confirmation').modal('hide');

     // and callback
     callback(true);
  });

  $('#modal-custom-confirmation button.cancel').off().on('click', function() {
     // close window
     $('#modal-custom-confirmation').modal('hide');
     // callback
     callback(false);
  });
}

以HTML格式

...
<div class="modal-footer">
        <button type="button" class="btn btn-success ok">Yes</button>
        <button type="button" class="btn btn-default cancel">No</button>
      </div>
...

如果您想要实际返回某些内容,则可以返回可以解析为truefalse的承诺。但是异步代码仍然是异步的。

答案 1 :(得分:1)

感谢大家的回答和评论。我终于解决了它。正如所建议的那样,我使用了一个名为Bootbox.js and use a customized confirm with alternate button text的库(不需要重新发明轮子,只需调整它以满足需要)

然后我合并了:Nuno ReisFabien MénagerRyan McGeary的答案,以创建解决方案。这就是我的所作所为:

  1. 为要使用此自定义确认的链接添加类作为标识符。在我的代码中,我使用了custom-confirm-link

  2. 将确认消息放入数据绑定data-confirmation

    所以我的链接现在看起来像这样:

    <a href="remove.php?id=3" class="custom-confirm-link" data-confirmation="Are you sure you want to remove this?" />
    
  3. 在页脚中,我将点击事件监听器添加到链接和类custom-confirm-link。内:

    • 检索了已触发的链接,其href及其确认消息通过数据绑定data-confirmation
    • 我对事件preventDefault()做了
    • 弹出一个bootbox.js使用自定义标签确认confirm(是)和cancel(否),
    • 利用其回调来处理结果
    • 只有点击了确认按钮是,我才能通过window.location.href模拟点击链接
  4. 那就是它。这是点击事件监听器代码:

    <script type="text/javascript">
        //  click event listener to links that requires custom confirm
        $('.custom-confirm-link').click(function(e){
    
            //  get link and its confirmation message
            var link                = $(this);
            var confirmationmessage = link.data('confirmation');
            var address             = link.attr('href');
    
            e.preventDefault();
    
            bootbox.confirm({
                title   : 'Confirm',
                message : confirmationmessage,
                buttons : {
                    confirm : {
                        label : 'Yes',
                        className: 'btn-success'
                    },
                    cancel : {
                        label : 'No',
                        className : 'btn-danger'
                    }
                },
                callback : function (result) {
                    if (result)
                    {
                        //  simulate click the link
                        window.location.href = address;
                    }
                }
            });
        });
    </script>