Serialize form does not handle the table

0

I have this function to check if something was changed in formulário , apparently it worked perfectly:

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

But it came up with the need for it to check table , if something was hidden in it, it does not take changes in this table :

<table class="table table-responsive table-hover" id="tabelaf" name="tabelaf">
                            <tbody>
                                @foreach (var item in Model.ProdutosFornecedores)
                                {
                                <tr class="tr item">
                                    <td>@item.FornecedorProduto.Id</td>
                                    <td>@item.FornecedorProduto.Nome</td>
                                    <td align="right">
                                        <a class="link-excluir" href="#" data-id="@item.Id" title="Excluir"><i class="fa fa-trash-o fa-lg"></i></a>&nbsp;
                                    </td>
                                </tr>
                                }
                            </tbody>
                        </table>

How do you compare it to this table too?

    
asked by anonymous 11.10.2018 / 13:58

1 answer

1
The serialize method of JQuery only serializes values of named elements (and that are not disabled), if you want to compare table data using serialize, you must give a name to all elements, as well as a name value (which will probably be the same as internal HTML).

Example :

<td name="id[]" value="@item.FornecedorProduto.Id">@item.FornecedorProduto.Id</td>
<td name="nome[]" value="@item.FornecedorProduto.Nome">@item.FornecedorProduto.Nome</td>

Edit: CORRECTION JQuery only serializes values of elements of type input, textarea or select, so even with a name the values of td will not be serialized. In case it would require an input of type hidden with the same values of td.

    
11.10.2018 / 21:24