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

183
Visualizações
Same AJAX call for multiple button on clicks on the same page

I'm working on a project with Django. I have multiple buttons on a page. When clicking one of these buttons I want to get the value of it without the page refreshing.

With the below code it does want I want except that it will only return the value of the first button on the page regardless of which button I click.

Please note that I have zero experience with JS which is why I'm struggling. I've looked at many, many threads and I can't figure it out.

<article class="media content-section">
  <div class="media-body">
    <div class="article-metadata">
      <h2>{{ ingredient.group }}</h2>
    </div>
    <button id="{{ ingredient.pk }}" class="testing" value="{{ ingredient.food_name }}">{{ ingredient.food_name }}</button>
  </div>
</article>
$(document).on('click', '.testing', function(e) {
  e.preventDefault();

  $.ajax({
    type: 'POST',
    url: '{% url "select" %}',
    data: {
      value: $('.testing').val(),
      csrfmiddlewaretoken: $('input[name=csrfmiddlewaretoken]').val(),
      // action: 'post',
      success: function(json) {
        console.log("success"); // another sanity check
        console.log($('.testing').val())
      },
    }
  })
})
about 4 years ago · Juan Pablo Isaza
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