javascript - 将 js 确认更改为 twitter Bootstrap 模式

标签 javascript jquery twitter-bootstrap modal-dialog confirm

我有一些 JavaScript 会向用户显示一个确认对话框,以确认他们对传入变量的操作。

但是,我必须将普通的确认对话框更改为 twitter-bootstrap 模式。

我已经尝试了几次,阅读了很多 SO 帖子并阅读了 twitter bootstrap 文档,但是我无法让它工作,因为我对 js 的了解不够好。我卡在了 Twitter Bootstrap 模式中变量的显示上。

我希望有人能给我一些指点来帮助我。

这是我当前的 js 对话框代码:

    function checkboxUpdated(checkbox, count, label, id) {

        if (checkbox.checked) {

            $('#menu_entry_' + id).show();

        } else {

            if (count > 0) {

                if (! confirm('{% trans "You have '+ count +' saved '+ label +'.\n\nIf you leave this option un-checked your saved '+ label +' will be deleted only after you update this page.\n\nAre you sure you want to delete your ' + count + ' saved ' + label +'?" %}')) {

                    checkbox.checked = true;
                    return;

                }

            }

            $('#menu_entry_' + id).hide();

        }

    }

编辑:根据评论中的要求添加#menu_entry_ 代码:

{% for id, menu_entry, selected, item_count in menu_entries %}

    <li id="menu_entry_{{ id }}" {% if not selected %}style="display:none;"{% endif %}>

        <a 

            {% if id == 4 %}

                href="{% url summary_details %}"

            {% elif id == 8 %}

                href="{% url objective_details %}"

            {% elif id == 12 %}

                href="{% url leading_employment_details %}"

            {% elif id == 16 %}

                href="{% url desired_occupation_details %}"

            ....

            {% elif id == 112 %}

                href="/"

            {% else %}

            href="/"

            {% endif %}

            onclick="showProgressAnimation();">

请注意,我需要将以下 js 确认代码转换为 twitter bootstrap 模态代码:

if (! confirm('{% trans "You have '+ count +' saved '+ label +'.\n\nIf you leave this option un-checked your saved '+ label +' will be deleted only after you update this page.\n\nAre you sure you want to delete your ' + count + ' saved ' + label +'?" %}')) {

最佳答案

您可以编写自己的 jQuery 插件。首先,将 Bootsrap 的模态组件添加到您的文档中。

<div class="modal fade" id="confirm" tabindex="-1" role="dialog" aria-labelledby="confirm-label" aria-hidden="true">
  <div class="modal-dialog modal-sm">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
        <h4 class="modal-title" id="confirm-label"></h4>
      </div>
      <div class="modal-body">
        <p class="message"></p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default dismiss" data-dismiss="modal"></button>
        <button type="button" class="btn btn-primary confirm" data-dismiss="modal"></button>
      </div>
    </div>
  </div>
</div>

基本上,插件应该在调用时显示模态,并在按下确认按钮时触发 confirm 事件,否则触发 dismiss

$.fn.confirm = function (options) {
  var settings = $.extend({}, $.fn.confirm.defaults, options);

  return this.each(function () {
    var element = this;

    $('.modal-title', this).html(settings.title);
    $('.message', this).html(settings.message);
    $('.confirm', this).html(settings.confirm);
    $('.dismiss', this).html(settings.dismiss);

    $(this).on('click', '.confirm', function (event) {
      $(element).data('confirm', true);
    });

    $(this).on('hide.bs.modal', function (event) {
      if ($(this).data('confirm')) {
        $(this).trigger('confirm', event);
        $(this).removeData('confirm');
      } else {
        $(this).trigger('dismiss', event);
      }

      $(this).off('confirm dismiss');
    });

    $(this).modal('show');
  });
};

我们可以对上述代码进行的改进是公开默认插件设置。

$.fn.confirm.defaults = {
  title: 'Modal title',
  message: 'One fine body&hellip;',
  confirm: 'OK',
  dismiss: 'Cancel'
};

使用示例:

$('#confirm').confirm().on({
  confirm: function () {
    console.log('confirm');
  },
  dismiss: function () {
    console.log('dismiss');
  }
});

在此处查看实例:http://jsfiddle.net/cdog/4q9t9pk5/ .

如果您不想编写自己的解决方案,您可以尝试现有的项目,例如:https://github.com/nakupanda/bootstrap3-dialog .它看起来像你要找的东西。此处提供文档和演示:http://nakupanda.github.io/bootstrap3-dialog/ .

关于javascript - 将 js 确认更改为 twitter Bootstrap 模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26199642/

相关文章:

javascript - 如何获取对象列表并等待它们完成?

javascript - 动态添加html select另一个选择框选项的onClick

javascript - 为什么这个确认功能没有继续下去?

jquery - 单击时更改图标(切换)

javascript - 如何使用 PhantomJS 和 node.js 进行抓取?

javascript - Bootstrap Multiselect - 没有单选按钮的单选

javascript - 通过 javascript 或 jquery 获取多个文件值无法正常工作

Jquery 不能重新加载

html - 在移动浏览器中左右移动页面

jquery - Bootstrap 下拉菜单不适用于 SlidesJS 3.0