javascript - 如何在不打开模式对话框的情况下为数据切换 ="modal"按钮设置自定义单击事件处理程序?

标签 javascript jquery html twitter-bootstrap

我有一个名为“HasIncreasePoint”的 $.post() 请求,如果从服务器返回的数据指示成功 (e.IsSuccess),我不想打开bootstrap模态对话框,完成点击事件处理。

$('a[data-toggle="modal"]').on('click', function (event) {
    $.post("@Url.Action("HasIncreasePoint")", function (e){
        if (e.IsSuccess) {
            alert("error!please not to open the modal!");
            event.preventDefault();
            event.stopPropagation();
            $('a[data-toggle="modal"]').off("click");
        }else{
            // From the clicked element, get the data-target arrtibute
            // which BS3 uses to determine the target modal
            var target_modal = $(e.currentTarget).data('target');
            // also get the remote content's URL
            var remote_content = e.currentTarget.href;

            // Find the target modal in the DOM
            var modal = $(target_modal);
            // Find the modal's <div class="modal-body"> so we can populate it
            var modalBody = $(target_modal + ' .modal-body');

            // Capture BS3's show.bs.modal which is fires
            // immediately when, you guessed it, the show instance method
            // for the modal is called
            modal.on('show.bs.modal', function () {
                // use your remote content URL to load the modal body
                modalBody.load(remote_content);
            }).modal();

            // and show the modal

            // Now return a false (negating the link action) to prevent Bootstrap's JS 3.1.1
            // from throwing a 'preventDefault' error due to us overriding the anchor usage.
            return false;
        }
    });
});

和 HTML 代码:

<a class="btn-check-in" href="@Url.Action("ReverseCard")"  data-toggle="modal" data-target="#myModal" id="btn-sign">
    <i></i><span>SignIn</span>
</a>

最佳答案

您可以删除 data-toggle="modal" 属性并绑定(bind)点击 .btn-check-in 类。
然后,每当您需要模式时,使用 javascript 打开它(就像您已经做的那样)

<a class="btn-check-in" href='@Url.Action("ReverseCard")' data-target="#myModal" id="btn-sign">
  <i></i><span>SignIn</span>
</a>

JS:

// set a flag to prevent multiple requests:
var waiting = 0;

$('.btn-check-in').on('click', function(event){
  event.preventDefault();
  if(!waiting){
    var myModal = $(this).data('target');
    var remote_content = this.href;
    $.post('@Url.Action("HasIncreasePoint")').done(function(e){
      if(!e.IsSuccess){

        // this part seems to be overdone, but I left it as is
        // as I don't know what is your reason of loading fresh content each time...
        $(myModal).on('show.bs.modal', function(){
          $(this).find('.modal-body').load(remote_content);
        }).modal('show');

      }else{
        // it was successful!
      }
      waiting = 0;
    });
  }
  waiting = "I'm waiting for $.post()";
});

关于javascript - 如何在不打开模式对话框的情况下为数据切换 ="modal"按钮设置自定义单击事件处理程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39349000/

相关文章:

javascript - 在选择下拉菜单中隐藏文件扩展名 (html/css)

javascript - Raphaël 的问题以及删除 svg 中的对象

javascript - Tailwind CSS 颜色不适用于下一个 js 组件。你如何应用背景颜色?

javascript - 遍历和替换类

javascript - 如何将计数功能应用于页面上的多个值?

javascript - P5.js、Node 和 Twitter API

javascript - 单击外部时隐藏列表(第 2 部分)

javascript - Jquery倒计时器失败

javascript - 如何将 svg 形状与 svg 文本合并

html - Css - 带有卷轴的垂直堆栈容器