【问题标题】:How can I toggle a bootstrap modal without using a button?如何在不使用按钮的情况下切换引导模式?
【发布时间】:2021-11-16 17:19:12
【问题描述】:

效果很好,用一个按钮切换我的引导模式(模式的 id 是“exampleModal”):

<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal">
  Launch demo modal
</button>

如果没有这个按钮,我怎样才能只调用切换方法?我有一个烧瓶/神社 for 循环,当出现错误时会执行/渲染。我正在努力实现某些目标 像这样:

{% for message in get_flashed_messages() %}
    exampleModal.toggle() <-- how can I achieve this?
{% endfor %}

在引导 javascript API 文档中它说 $('#myModal').modal('show'),但我找不到在我的模板中调用它的正确语法。

【问题讨论】:

    标签: html jquery twitter-bootstrap


    【解决方案1】:

    在 Bootstrap 5 中使用 JavaScript 完成。不再需要 jQuery。

    var modalPopup = new bootstrap.Modal(document.getElementById("exampleModal"), {});
    modalPopup.show();
    

    Codeply

    【讨论】:

    • 谢谢。它仍然不能完全工作。在{% for message in get_flashed_messages() %} 中,我将您的建议行写在函数myFunction 中,我可以通过按钮&lt;button type="button" onclick='myFunction()'&gt;Click Me!&lt;/button&gt; 调用该函数,这样就可以了。但是当我将&lt;script&gt;myFunction()&lt;/script&gt; 放入for 循环时,该函数永远不会被调用。这很奇怪,因为按钮有效,例如 &lt;script&gt;alert(123)&lt;/script&gt; 被执行,而不是 &lt;script&gt;myFunction()&lt;/script&gt;
    • myFunction() 需要被调用
    • 但是为什么&lt;script&gt;alert(123)&lt;/script&gt; 工作,而&lt;script&gt;myFunction()&lt;/script&gt; 什么都不做?
    • codeply.com/p/T875Mm6tTP 我修改了你的例子。无法让&lt;script&gt;myFunction()&lt;/script&gt; 工作。
    • 请接受它,因为它回答了您的问题 “如何在不使用按钮的情况下切换引导模式?” 当您实际调用该函数时,它再次正常工作:@987654323 @
    猜你喜欢
    • 2015-05-23
    • 1970-01-01
    • 2020-04-20
    • 1970-01-01
    • 2018-04-06
    • 2015-12-22
    • 1970-01-01
    • 2020-03-02
    • 1970-01-01
    相关资源
    最近更新 更多