Modal Bootstrap open content from another page

1

I would like that when I click the button, I open the contents of another page in a Bootstrap modal. I have the following code below:

<script src="js/jquery.min.js"></script>
<button class="btn btn-success" id="btnImprimir" title="Clique para imprimir sua carteira" data-toggle="modal"><i class="fas fa-print"></i> Imprimir</button>
    <div class="modal drag" id="modalImprimir" tabindex="-1" role="dialog" aria-labelledby="" aria-hidden="true">
              <div class="modal-dialog">
                  <div>
                      <div id="tela">
                      </div>
                  </div>
              </div>
          </div>
    <script>
              $("button").on('click',"#btnImprimir", function(){
                alert('aqui');
                  $.post('carteira-imprimir.php', function(retorno){
                         $("#modalImprimir").modal({ backdrop: 'static' });
                         $("#tela").html(retorno);
                  });
              });
    </script>

And the portfolio-print.php page

<div class="modal-content">
    <div class="modal-header bg-primary text-white">
      <h5 class="modal-title" id="exampleModalLabel" style="font-weight: bold"><i class="fas fa-address-card"></i> CARTEIRA ESCOLAR</h5>
      <button type="button" class="close" data-dismiss="modal" aria-label="Close">
        <span aria-hidden="true">&times;</span>
      </button>
    </div>
    <div class="modal-body">
     Conteúdo
    </div>
</div>

Just clicking does not open the modal or alert () that I put to test. How do I fix this?

    
asked by anonymous 04.12.2018 / 17:26

1 answer

1

The click selector is incorrect using "button" , because the element with the #btnImprimir id is the button itself.

Or you change to $(document) :

$(document).on('click',"#btnImprimir", function(){...

Or use the element itself in the selector using id:

$("#btnImprimir").on('click', function(){...
    
04.12.2018 / 17:51