Rails Rails 自定义 jquery_ujs 的 confirm 确认对话框

huacnlee · March 15, 2016 · 2434 hits

Rails 可以很方便的给按钮加上提交前的确认对话框:

<a href="#" data-confirm="确定要提交么?" class="btn btn-primary">提交</a>
<a href="#" data-confirm="确定要删除这个信息么?" class="btn btn-danger">删除</a>

但它调用的是浏览器默认的确认对话框,如果想要定制怎么办呢?

可以用这样简单的方法:

我这里用的是 Bootbox 这个对话框库 http://bootboxjs.com/

// https://gist.github.com/huacnlee/84d13ea64cc8ccb2d4b0
$.rails.allowAction = function(element) {
  var message = element.attr('data-confirm');
  if (!message) { return true; }

  var opts = {
    title: "确认提交",
    message: message,
    callback: function(ok) {
      if (ok) {
        element.removeAttr('data-confirm');
        element.trigger('click.rails')
      }
    }
  };

  bootbox.confirm(opts);

  return false;
}
No Reply at the moment.
You need to Sign in before reply, if you don't have an account, please Sign up first.