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

195
Visualizações
Submit button getting disabled automatically after onSubmit event returns false with alert. Following onchange event also not able to enable it

Submit button getting disabled automatically after onSubmit event returns false with alert. Later on, onchange event also not able to enable it.

Below is the javascript and HTML

On submitting form without checkbox, an alert is popped and the submit button gets disabled. On checking the box Submit button cant enable it back. How can i ensure submit button to remain enabled or onchange event to enable it?

Any leads are appreciated.

            
<script type="text/javascript">
  function checkme() {
        if (!document.form.agree.checked) {
            missinginfo = "You must agree to the condition\n Please tick the checkbox and try again.";
            alert(missinginfo);
            return false;
        }
        else {
            return true;
        }
    }
</script>
            <form name="form" id="form"  action="/gl" onSubmit="return checkme();" method="post">
                
                           <input type="checkbox" id="agree" onchange="document.getElementById('submit-form').disabled = !this.checked;"/> I AGREE
                           
                           <div class="row">
                              <div class="col-sm-4 col-sm-offset-8 col-md-3 col-md-offset-9 input-group">
                                  <button type="submit" value="Submit" class="btn btn-primary" id="submit-form">Submit</button>
                               </div>
                           </div>

`

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

0

First, you should consider moving your event listening to your script instead of using onSubmit()

Then you can select your input and button on the script, and add an event listener to your input, so every time it changes, it checks if the checkbox is checked or not (checkBox.checked). If it is not checked, it disables the button (button.disabled = !checkBox.checked;)

const checkBox = document.getElementById("agree");
const button = document.getElementById("submit-form");

checkBox.addEventListener("input", () => {
  button.disabled = !checkBox.checked;
});
<form name="form" id="form" action="/gl" onSubmit="return checkme();" method="post">
  <input type="checkbox" id="agree" /> I AGREE
  <div class="row">
    <div class="col-sm-4 col-sm-offset-8 col-md-3 col-md-offset-9 input-group">
      <button disabled type="submit" value="Submit" class="btn btn-primary" id="submit-form">
            Submit
          </button>
    </div>
  </div>
</form>

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