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

106
Visualizações
Selectpicker cancel change value event

Consider the following event:

$(`#selectpicker).on('change', function(){
if(condition) {} else 
{
  //canceel event change
}
});

How to cancel the change event based on specific condition therefore maintaining the same select picker value.

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

0

Is this what you are looking for? Set a variable for the current value and if an undesired value is selected we set the value of the select field back to the recent value.

<select id="selectpicker">
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
</select>

let currentValue = $('#selectpicker').val();
$('#selectpicker').on('change', function(){
  let newValue = $(this).val();
  
  if(newValue == 3){
    console.log("3 is not allowed");
    $(this).val(currentValue);
    return;
  }
  currentValue = $('#selectpicker').val();
})

You could also disable certain options with "disabled" keyword, but keep in mind that this can be easily bypassed from developer tools.

<option disabled value="3">3</option>
about 4 years ago · Juan Pablo Isaza Relatório

0

While you can stop the execution of the callback with return false, the value will already have changed when this event is triggered. If you want to return to the previous value, you have to store it somehow, either in a variable or in a data property on the element. Then you can set the value back in your condition, or update the variable when your cancel-condition is not met.

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