Modal Bootstrap open content from another page

Asked

Viewed 615 times

1

I would like to click the button, open in a modal Bootstrap the contents of another page. 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 page wallet-print.php

<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>

Only when clicking, it does not open the modal nor the Alert() I put for test. How do I correct this?

  • 2

    Your selector is incorrect. Instead of $("button").on(... should be $(document).on(...

  • Hello Sam. Perfect!! It worked! Thank you so much again! If you want to put as an answer, I click accept as the correct answer.

1 answer

1


Click selector is incorrect using "button", because the element with id #btnImprimir 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(){...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.