Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

140
Visualizações
Bootstrap modal running once plus once for every previous time it was opened

I'm making a simple Gazetteer application. I open a modal by clicking on a marker which opens up with a photo of the city the marker is on.

It works as intended the first time I select a city. Any subsequent times clicking any marker will loop through the modal code as many times as it has been opened previously.

markers.on('click', function (e) {
    city = e.layer.options.title
    $('#myModal').on('show.bs.modal', function (event) {
        //BELOW THIS LINE IS WHERE LOOPING OCCURS
        var modal = $(this);
        $.when(runAjax('City Reference', 'source/php/getCityInfo.php', 'POST',city))
        .then(function() {
             cityReference = ajaxResponse.place_id;
             $.when(runAjax('Photo Reference', 'source/php/getPhotoRef.php', 'GET', cityReference))
             .then(function() {
                 photoReference = ajaxResponse.photoR;
                 modal.find('.modal-body').html('<img src=https://maps.googleapis.com/maps/api/place/photo?photoreference=' + photoReference + '&input=green%20lanes&sensor=false&types=(regions)&key=<<<HIDDEN>>>&maxwidth=400&maxheight=400></div>');
            });
        });
        
        modal.find('.modal-title').html('<b>' + city + '</b>');
    }) // ABOVE THIS LINE IS WHERE LOOPING STOPS
    $('#myModal').modal('show');
});

I have no understanding as to why this is happening.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

As per Ouroborus comment, I just moved the show.bs.model to underneath the click handler:

  
  
    $('#myModal').on('show.bs.modal', function (event) {
        
        var modal = $(this);
        $.when(runAjax('City Reference', 'source/php/getCityInfo.php', 'POST',city))
        .then(function() {
             cityReference = ajaxResponse.place_id;
             $.when(runAjax('Photo Reference', 'source/php/getPhotoRef.php', 'GET', cityReference))
             .then(function() {
                 photoReference = ajaxResponse.photoR;
                 modal.find('.modal-body').html('<img src=https://maps.googleapis.com/maps/api/place/photo?photoreference=' + photoReference + '&input=green%20lanes&sensor=false&types=(regions)&key=<<<HIDDEN>>>&maxwidth=400&maxheight=400></div>');
            });
        });
        
        modal.find('.modal-title').html('<b>' + city + '</b>');
    });
    markers.on('click', function (e) {
        city = e.layer.options.title
        $('#myModal').modal('show');
    });
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda