Override the preventDefault() method

Asked

Viewed 402 times

2

Is there any way to "undo" the method preventDefault()? I have this little code that solves this for me, but I was wondering if there’s any more efficient way to do it.

var $form = $('form');
$form.data('working', false);
$form.data('canSubmit', false);

$form.submit(function (e) {
    if (form.validations.form($form)) {
        if (!$form.data('working')) {
            $form.data('working', true);

            if ($form.data('canSubmit')) {
                // make the post here
            } else {
                e.preventDefault();
                $.get(BASE + 'file.txt', function (response) {
                    var lastPartnerSent = response;
                    var nextPartner = '';
                    switch (lastEmailSent) {
                        case 'Empresa 1':
                            nextPartner = 'Empresa 2';
                            break;
                        case 'Empresa 2':
                            nextPartner = 'Empresa 3';
                            break;
                        default:
                            nextPartner = 'Empresa 1';
                    }

                    $('input.i_parceiro').val(nextPartner);
                    $form.data('canSubmit', true);
                    $form.submit();
                });
            }
        }
    } else {
        $form.data('working', false);
        e.preventDefault();
        return false;
    }
});

This happens because I have to read and write to a file after validating the fields and only after that post to the page. The post cannot be done by ajax.

The question is basically whether it is possible to divide that form by 3 steps, ie:

1º validate fields, for example name, email, etc (synchronous)

2º depending on the data already validated, make an ajax that will add a "partner" field to the form (asynchronous)

3º submit the form by post

1 answer

2


You can use .on() and .off(), that is to add the event observer and then remove if the validation gives true.

The idea would be like this (note that it is pseudo-code to give an idea of how to do):

$form.on('submit', verificador);

function verificador(e){
    if (!validouCerto) return e.preventDefault(); // ou outros avisos caso não valide

    // aqui código caso valide (pois o "if" anterior faria "return" caso não valide)
    $form.off('submit', verificador);
    $form.submit();
}

The reason to use $form.on('submit', verificador); is so we can use the .off() which must indicate the same function in order to be removed.

After the information gathered in the question/chat and comments I suggest you use so:

var $form = $('form');
$form.on('submit', verificador);

function verificador(e) {
    var validou = form.validations.form($form);
    e.preventDefault();
    if (!validou) return false;

    $.get(BASE + 'file.txt', function (response) {
        var lastPartnerSent = response;
        var nextPartner = '';
        switch (lastEmailSent) {
            case 'Empresa 1':
                nextPartner = 'Empresa 2';
                break;
            case 'Empresa 2':
                nextPartner = 'Empresa 3';
                break;
            default:
                nextPartner = 'Empresa 1';
        }

        $('input.i_parceiro').val(nextPartner);
        $form.off('submit', verificador);
        $form.submit();
    });
}

Browser other questions tagged

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