Disable disabled from form

Asked

Viewed 51 times

-2

I have this function, where it checks the data, to exit the page, what happens that some data get like disabled according to the data reported, and I cannot verify the data.

  $(function () {
    var init_form = $('#Novo').serialize();
    $(':submit').click(function () { window.onbeforeunload = null; });

        window.onbeforeunload = function () {
        var check_form = $('#Novo').serialize();
        console.log(init_form);
        console.log(check_form);
        if (check_form === init_form) return null;
        return 'Os dados do formulário não foram salvos, deseja permanecer nesta página?';
    };
});

I tried to add this line in the method $("#Novo :input").prop('disabled', false); but it didn’t work either.

 $(function () {
    var init_form = $('#Novo').serialize();
    $(':submit').click(function () { window.onbeforeunload = null; });
         $("#Novo :input").prop('disabled', false);
    window.onbeforeunload = function () {
        var check_form = $('#Novo').serialize();
        console.log(init_form);
        console.log(check_form);
        if (check_form === init_form) return null;
        return 'Os dados do formulário não foram salvos, deseja permanecer nesta página?';
    };
});

How can I make sure that all data is checked, regardless of whether the property disabled or readonly be as true.

  • But if you disable the inputs disabled and the user cancels the beforeunload, these inputs will be active. It wouldn’t be weird?

  • Tried to put the $("#Novo :input").prop('disabled', false); within the function onbeforeunload ?

  • Is there any way to "circumvent" this, I put it as readonly but even so, some still does not appear in the comparison.

1 answer

1


You can create an auxiliary function that will recover the data from the disabled inputs and thus not needing to disable any field as the function serialize() does not recover these data, follow the example below.

$(function () {
    var init_form = $('#Novo').serialize();
    $(':submit').click(function () { window.onbeforeunload = null; });
    window.onbeforeunload = function () {

        // Variável que irá guardar os dados de inputs disabilitados
        var inputsDisabled = InputsDisabled('#Novo');

        var check_form = $('#Novo').serialize() + inputsDisabled;
        console.log(init_form);
        console.log(check_form);
        if (check_form === init_form) return null;
        console.log('Os dados do formulário não foram salvos, deseja permanecer nesta página?');
        return false;
    };
});

Function to recover disabled fields

function InputsDisabled(form) {
    const input = $(`${form} input:disabled`);
    let params = '';
    $.each(input, function (key, val) {
        params += `&${val.name}=${val.value}`;
    });
    return params;
}

Browser other questions tagged

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