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

204
Visualizações
jQuery multiple events including an IF statement

Does anybody know how to combine these two events in jQuery?

The closest I could get was having 2 duplicate scripts that respond to 2 different events, which in my opinion is absolutely terrible. But it's the only way I could think of to do this for now. Both scripts do the exact same thing (or A LOT of things actually). I know jQuery has an option to add multiple event handlers to the same element, but how does this work if one of the events contains an if condition?

This runs when the user clicks on a radio button in a radio group:

$(document).ready(function() {

  // When radio button is clicked
  $("input[name='select']").click(function() {
    var select = $("input[name='select']:checked").val();
    $.ajax({
      type: "POST",
      url: "script.class.php",
      data: { select: select },
      cache: false,
      success: function(data) {
        // A lot of things will happen here...
      },
      error: function(xhr, status, error) {
        console.error(xhr);
      }
    });
  });     
});

This runs IF the radio group already has a radio button selected:

$(document).ready(function() {

  // If value is already present
  if ($("input[name='select']:checked").val()) {
    var select = $("input[name='select']:checked").val();
    $.ajax({
      type: "POST",
      url: "script.class.php",
      data: { select: select },
      cache: false,
      success: function(data) {
        // The same things as in the previous script will also happen here...
      },
      error: function(xhr, status, error) {
        console.error(xhr);
      }
    });
  }
});
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Put all the common code in a function and then call it

$(document).ready(function() {

  // When radio button is clicked
  $("input[name='select']").click(commonCode);
  if ($("input[name='select']:checked").val()) {
    commonCode();
  }
  function commonCode(){  
    var select = $("input[name='select']:checked").val();
    $.ajax({
      type: "POST",
      url: "script.class.php",
      data: { select: select },
      cache: false,
      success: function(data) {
        // A lot of things will happen here...
      },
      error: function(xhr, status, error) {
        console.error(xhr);
      }
    });
  }     
});
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