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

查看:30
本文介绍了当您关闭并重新打开模态时,shows.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();
});

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

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