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

175
Visualizações
JQuery how to do AJAX call when checkbox is clicked and send its state?

I want to make an AJAX call when the user clicks on a checkbox. The call sends an id along with the checkbox state. Right now when I click my checkbox it creates two AJAX calls, one with the first id in the list and one is correct one I clicked.

How can I send one ajax call of the checkbox I clicked?

I have hundreds of rows with customer id 1000, 1001, 1002 etc. Right now the AJAX call is always 1000 and an ajax call with the customer id I clicked. Lets say I click the checkbox on row with customer id 1005 then the AJAX call right now is:

testurl&cid=1000&checkbox=1
testurl&cid=1005&checkbox=1

$document.on('change', '.toggle-checkbox', function() {
  var $this = $(this);
  var customerid = $row.data('customerid');
  var checkState = '';

  if ($(this).is(':checked')) {
    checkState = 1;
  } else {
    checkState = 0;
  }
  
  $.ajax({
    url: base_url + "testurl",
    type: "GET",
    data: {
      cid: customerid,
      checkbox: checkState
    },
    success: function(result) {
      console.log('succesful change');
    },
    error: function(xhr, status, error) {
      console.log('unsuccesful change');
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<tr class="test-row" data-customerid="1000">
  <td data-content="Customer ID" class="" style="max-width:700px">1000</td>
  <td class="toggle-checkbox" data-content="">
    <div class="custom-control custom-switch no-click">
      <input type="checkbox" checked="checked" class="custom-control-input toggle-checkbox" id="">
      <label class="custom-control-label" for="">&nbsp;</label>
    </div>
  </td>
</tr>

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

0

why don't try this

  url: base_url + "testurl?cid="+ customerid+"&checkbox="+checkState,

and remove "data:..." from ajax

if it still is not working then

<input type="checkbox" id="row1000" checked="checked" class="custom-control-input toggle-checkbox" >

javascript

 var customerid = this.id.substring(3);
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