run Function even if input is disable - Jquery -javascript

Asked

Viewed 52 times

-1

wanted to run an Alert if Submit input even if input is disabled follows the code.

<input type="submit" value="Cadastrar" class="col-md-12 col-xs-12 col-lg-12" id="submitCadastro" disabled="">

tried with the event click and with the event mouseente and only occurs when the input button is enabled,

  • When the button is disabled, it will not perform any function. You must enable the button (by removing the disabled) and after firing click disable function. I still don’t understand your question.

  • 1

    hi @jeangatto, I have a form and the button is enabled when the person agrees to the terms so I wanted to put an alert or a message saying that she forgot to agree to the terms in case she click the button without agreeing to the terms

1 answer

0


I made a small solution with jQuery to help you, it is not necessary to block the submit button, follow the code below:

    $(function () {

    var $form = $("#ID_FORMULARIO"),
        $ckTermosDeUso = $("#ID_CHECKBOX"),
        $btnSubmeter = $("#ID_BOTAO_SUBMIT");

    // Evento do Click do botão submeter
    $btnSubmeter.on("click", function (e) {

        // Evitando de executar o envio real do formulário.
        e.preventDefault();

        // Verificando se o checkbox foi marcado.
        if ($ckTermosDeUso.is(":checked")) {

            // Bloqueando o botão submeter para evitar vários click's
            $btnSubmeter.attr("disabled", "disabled");

            // Submetendo o formulário.
            $form[0].submit();
            return true;
        }
        else {

            window.alert("Você deve aceitar os termos de uso!");
            return false;
        }
    });

});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.