我的Bootstrap模式需要点击两次才能打开

时间:2019-06-26 22:01:15

标签: javascript php html ajax

最近,我遇到了这个错误,我只是不知道如何解决。

当我单击“ Ver Receita”按钮时,它什么都没有显示,但是如果我再次单击,则模式会打开。我认为问题在于,模态位于外部文件中(由于模态是动态的)。

带有按钮的HTML

O(n)

此按钮触发 verReceita.php 中的div显示

<button type="button" 
        class="btn btn-success text-center mb-1"
        data-toggle="modal" 
        data-target="#receita" 
        onclick="verReceita(<?php echo $registo['id'];?>)">
Ver Receita
</button>

verReceita.php

<div class="modal fade" id="receita">
    <div class="modal-dialog modal-lg">
    <div class="modal-content">
        <div class="modal-header">
        <h1 class="page-header text-center">Detalhes da Receita</h1>
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        </div>

   <div class="modal-body">
      <table class="table table-striped table-bordered border border-dark">

        <tbody>

        </tbody>

      </table>
      <h2>Ingredientes</h2>
      <p>Quero que aqui apareçam os ingredientes associados à receita</p>
      </div>
      <div class="modal-footer">
        <button class="btn btn-success" type="button" class="btn pull-left" data-dismiss="modal">Cancelar</a></button>
      </div>
  </div>
</div>
</div> 

我的verReceita.php发出AJAX请求,并将参数发送到外部 verReceita.php

关于解决此两键问题的任何建议吗?

1 个答案:

答案 0 :(得分:0)

之所以发生这种情况,是因为您的按钮上有一个onClick侦听器,以及Bootstrap模式触发器。您可以删除onClick侦听器,也可以从onClick侦听器调用的verReceita函数内触发模式。下面的代码段显示了两个示例:

function verReceita() {
  $('#receita').modal('toggle');
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<button type="button" class="btn btn-success text-center mb-1" data-toggle="modal" data-target="#receita"> <!-- onclick="verReceita(<?php echo $registo['id'];?>)"> -->
Ver Receita
</button>

<button type="button" class="btn btn-info text-center mb-1" onclick="verReceita()">
Ver Receita 2
</button>

<div class="modal fade" id="receita">
  <div class="modal-dialog modal-lg">
    <div class="modal-content">
      <div class="modal-header">
        <h1 class="page-header text-center">Detalhes da Receita</h1>
        <button type="button" class="close" data-dismiss="modal">&times;</button>
      </div>

      <div class="modal-body">
        <table class="table table-striped table-bordered border border-dark">

          <tbody>

          </tbody>

        </table>
        <h2>Ingredientes</h2>
        <p>Quero que aqui apareçam os ingredientes associados à receita</p>
      </div>
      <div class="modal-footer">
        <button class="btn btn-success" type="button" class="btn pull-left" data-dismiss="modal">Cancelar</button>
      </div>
    </div>
  </div>
</div>

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>