当您关闭并重新打开模态时,showd.bs.modal会多次触发 [英] shown.bs.modal fires multiple times when you close and reopen modal

查看:474
本文介绍了当您关闭并重新打开模态时,showd.bs.modal会多次触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我摆弄了一个小提琴,以说明我目前面临的问题.因此,每次我关闭和打开模态时,shown.bs.modal也会触发多次.在这种提琴演奏中,每次您关闭和打开模式时,警报的数量也会增加(假设只触发一次). http://jsfiddle.net/j36h2/1/

I have made a fiddle illustrating the issue I am facing at the moment. So every time I close and open a modal, shown.bs.modal also fires multiple times. In this fiddle, every time you close and open a modal, number of alerts also increases (when it's supposed to trigger only once). http://jsfiddle.net/j36h2/1/

function openTestModal(){
    $('#testModal').modal({
        keyboard: false,
        backdrop: 'static'
    });

    $('#testModal').on('shown.bs.modal', function (e) {
        alert('');
    });
}

$('.testButton').click(function(){
    openTestModal();
});

推荐答案

您需要从点击事件中提取警报功能:

You need to extract your alert function out from your click event :

http://jsfiddle.net/SyCNj/2/

提取:

function openTestModal(){
    $('#testModal').modal({
        keyboard: false,
        backdrop: 'static'
    });
}

$('#testModal').on('shown.bs.modal', function (e) {
   alert('');
});

$('.testButton').click(function(){
    openTestModal();
});

这篇关于当您关闭并重新打开模态时,showd.bs.modal会多次触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆