jquery trigger for submit form results infinite lo

2019-08-09 06:29发布

When starts my plugin it runs a trigger for loading data from mvc c# controller just once but the page is refreshing infinite (loop). When I press submit button is running ok.

Here is the problem:

    if (opcoes) {
        $.extend(defaults, opcoes);
                $('#formPaginacao').trigger('submit');
            }

Rest of code (works when I press button submit):

    return this.each(function () {

        function postProcessing(data) {
            dados = data;
            p.totalRegistros = dados.length;
            defaults.totalPaginas = Math.ceil(p.totalRegistros / defaults.registrosPorPagina);
            defaults.registroAtual = 0;
            renderiza();
        }
        $('#formPaginacao').submit(function (e) {
            e.preventDefault();
            dados = null;
            getValues();
            e.stopPropagation();
        });

        function getValues() {
            var dadosPesquisaForm = $('#formPaginacao').serialize();
            var dadosPesquisaJson = JSON.stringify(dadosPesquisaForm);
            var dadosEnvio = JSON.parse(dadosPesquisaJson);
            $.ajax({
                type: 'POST',
                url: defaults.controleCarregaDados,
                data: dadosEnvio,
                dataType: 'json',
                cache: false,
                success: function (data) { postProcessing(data); },
                async: true,
                error: function (erro) {
                    alert('erro ajax=' + erro)
                }
            });
        };

{...}

1条回答
我命由我不由天
2楼-- · 2019-08-09 07:04

you have too much built into this. change your submit button to just a button

<input type="button" id="formPaginacao" value="Submit" />

then you just need

$('#formPaginacao').on('click', function (e) {
    $.ajax({
        type: 'POST',
        url: defaults.controleCarregaDados,
        data: $('#formPaginacao').serialize(),
        dataType: 'json',
        cache: false,
        success: function (data) { postProcessing(data); },
        async: true,
        error: function (erro) {
            alert('erro ajax=' + erro)
        }
    });
});

This shouldn't be in an each statement either. just put it in

$(document).ready(function(){
    //button click here
});

form.serialize will pass all of the values from your form back to the controller. the input on your controller should be the same model that you have defined on your view

[HttpPost]
public ActionResult formPaginacao(ModelType model){
    //do something with the data
    //return json back tot he view
}
查看更多
登录 后发表回答