切换模态后出现Javascript错误

javascript error after toggle modal

本文关键字:Javascript 错误 模态      更新时间:2023-09-26

我只是新的引导,我得到一个javascript错误,我不知道如何管理。

我有一个在第一列中带有复选框的数据表。在选择了一些复选框之后,您可以单击两个按钮,打开带有不同消息的模态,用于接受或取消消息。但是如果没有复选框被选中,我会显示一个信息警报(也是bootstrap),我需要"关闭"模态。

这是js代码:
$('#actionsModal').on('show.bs.modal', function(e) {
    var action = $(e.relatedTarget).data('id');
    var total = $('input[id^="checkboxRol"]:checked').length;
    var message = "", object = "", confirmation = "";
    if(total == 0){
        $('.alert-info').show();

        // -----------------------------------------------------
        // this is the line giving the error!!!
        $('#actionsModal').modal("toggle");
        // -----------------------------------------------------

    }
    else if(total > 1){
        message = "Va a " + action + " los " + total + " objetos seleccionados:";
        confirmation = "¿Desea realizar esta operación?";
    }
    else{
        message = "Va a " + action + " el objeto:";
        object = "-obtener nombre-<br><br>";
        confirmation = "¿Desea realizar esta operación?";
    }
    $(e.currentTarget).find('span[id="message"]').html(message);
    $(e.currentTarget).find('span[id="object"]').html(object);
    $(e.currentTarget).find('span[id="confirmation"]').html(confirmation);
});
js错误是:Uncaught RangeError: Maximum call stack size exceeded

任何想法?也许我可以使用另一种类型的引导实体,并使用简单的按钮或链接点击模态。

提前感谢!

如果你想关闭模态,请使用

$('#actionsModal').modal("hide");

而不是toggle。我不确定它是否有助于你的情况下,但它可以防止不必要的无限递归在show.bs.modal事件(这是你的问题)

解决!

最后只需要替换"show.bs"。modal' event by 'show .bs. '事件模态"

现在行:

$('#actionsModal').modal("toggle");

不创建无限调用。

由于@JesusQuintana