【问题标题】:How to make a confirmation modal with Remodal如何使用 Remodal 制作确认模式
【发布时间】:2015-10-26 18:57:46
【问题描述】:

我使用 Remodal (https://github.com/VodkaBears/Remodal#remodal) 来制作一些模式,通常我只使用警报模式,但现在我想使用确认模式。

我制作了一个带有确认模式的删除按钮,我有以下代码:

$('#delete_button').click(function() {
    $('[data-remodal-id = modal-delete]').remodal().open();
    $(document).on('confirmation', '#modal-delete', function () {
        alert('Confirmation button is clicked');
    });
    $(document).on('cancellation', '#modal-delete', function () {
        alert('Cancel button is clicked');
    });
});

<div class="remodal" data-remodal-id="modal-delete">
    <div class="h1_modal">Delete confirmation</div> 
    <div class="p_modal">Are you sure?</div>
    <button data-remodal-action="cancel" class="remodal-cancel">Cancel</button>
    <button data-remodal-action="confirm" class="remodal-confirm">OK</button>
</div>

当我单击 ID 为 delete_button 的按钮时,模式正在打开,但是当我选择取消或确定时,我没有看到任何警报。我怎样才能让它工作?

亲切的问候,

阿里

【问题讨论】:

  • 我可以立即看到几个问题:我在您的 HTML 中看不到 #delete_button 元素,并且您的事件委托似乎毫无用处,除非您的帖子中缺少支持代码。

标签: jquery modal-dialog confirmation remodal


【解决方案1】:

我的解决方案:

HTML:

 <div data-remodal-id="alert">
  <a data-remodal-action="cancel" class="remodal-cancel">X</a>

  <div id="confirmation-box">
    <p>You didn't create your alert. Do you really want to close this form?</p>
    <a href="#" data-remodal-action="close" class="btn remodal-close">
      <span>Close</span>
    </a>
    <a href="#" data-remodal-action="confirm" class="btn remodal-confirm">
      <span>No</span>
    </a>
  </div>

JS:

var remodalInst = $('[data-remodal-id=alert]').remodal({
    closeOnConfirm: false
});

$(document).on('cancellation', '.remodal', function () {
    // disable other close options while modal is open
        remodalInst.settings = {
            closeOnCancel: false,
            closeOnEscape: false,
            closeOnOutsideClick: false
        }
   // show confirmation window
        $('#confirmation-box').show();
   // hide confirmation window after clicking "no" without hiding whole modal
        $(document).on('confirmation', '.remodal', function () {
            $('#confirmation-box').hide();
        });
});

【讨论】:

    【解决方案2】:

    在删除点击事件之外添加取消和确定点击事件:

    $('#delete_button').click(function() {
        $('[data-remodal-id = modal-delete]').remodal().open();
        // delete logic
    });
    
    $(document).on('click', '.remodal-confirm', function () {
            alert('Confirmation button is clicked');
    });
    $(document).on('click', '.remodal-cancel', function () {
            alert('Cancel button is clicked');
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-04
      • 1970-01-01
      • 1970-01-01
      • 2018-08-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多