bootstrap alert-discresable在django中无法工作

daolsyd0  于 2022-12-14  发布在  Go
关注(0)|答案(2)|浏览(127)

下面是我HTML代码

<div class="msg">
{% if messages %}
  {% for message in messages %}
    <div class="alert alert-dismissible alert-success">
      <button type="button" class="close" data-dismiss="alert">
      ×
      </button>
      <strong>{{message}}</strong>
    </div>
 {% endfor %}
{% endif %}
</div>

表单提交后显示消息,但x按钮无法折叠,如何解决,请帮助我。

jjhzyzn0

jjhzyzn01#

如果bootstrap文档中指出的默认方式不适合你,你可以尝试在你的按钮上添加一个id,然后使用javascript关闭它。我在我的例子中使用的是jQuery。同时检查你没有遗漏任何需要的cdn

$( "#alerto" ).click(function() {
  $( ".alert" ).alert('close');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet" />
<div class="msg">
  {% if messages %} {% for message in messages %}
  <div class="alert alert-warning alert-dismissible fade show" role="alert">
    <strong>Holy guacamole!</strong> You should check in on some of those fields below.
    <button type="button" class="close" id="alerto" data-dismiss="alert" aria-label="Close">
    <span aria-hidden="true">&times;</span>
  </button>
  </div>
  {% endfor %} {% endif %}
</div>
iovurdzv

iovurdzv2#

我在Django模板消息中遇到了同样的问题,我的问题通过在模板中添加以下脚本得到了解决:

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudfare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js" integrity="sha384-wHaiFfRlMFy6i5SRaxvfOCifBUQy1xHdJ/yoi7FRNXMRBu5WHdZYu1hA6ZOblgut" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js" integrity="sha384-B0UglyR+jN6CkvvICOB2joaf5I4l3gm9GU6Hc1og6Ls7i6U/mkkaduKaBhlAXv9k" crossorigin="anonymous"></script>

您可能还想添加此链接:

<link rel="stylesheet" href="https://bootswatch.com/4/pulse/bootstrap.min.css">

相关问题