Update fields with Javascript

Asked

Viewed 980 times

5

The problem

I’m making a requisition Ajax to return some data from a table. He arrives and enters this function to start the foreach of the information, but two of them are getting "lost" on the way. I gave a warning on value['outras_atividades'] before he entered the if, and he showed the text of that field normally, but went inside the if that he became like undefined, that is, I cannot fill the text fields with these values.

My Code

Ajax

$('#verificaEntidade').click(function () {
        $.ajax({
            type: "POST",
            dataType: "json",
            url : "retornaEntidade",
            data: $('#formVerifEnt').serialize(),
            success : function(msg){
                if(msg.status == 0) {
                    alert("O formulário dessa entidade ainda não foi preenchido !");
                }
                else {
                    msg.errorMsg.forEach(ShowResults);
                }
            },
            error:function (xhr, ajaxOptions, thrownError) {
                 alert("Erro no Processamento dos Dados. Entre em contato com o setor de Tecnologia e informe a mensagem abaixo:\n"+xhr.responseText);
            }
        });

    });

Function to mark form fields

function ShowResults(value, index, ar) {
        var i = 0;

        /*Faz a verificação para atualizar os checkboxes das atividades realizadas*/
        if(value['codigo_atividade']) {
            for(i = 1; i <= 11; i++) {
                if(value['codigo_atividade'] == i) {
                    $('#atv'+ i).prop('checked', true);
                    if(value['codigo_atividade'] == 11) {
                        $('input[name=txtOutrasAtividades]').val(value['outras_atividades']);
                    }
                }
            }
        }

        /*Faz a verificação para atualizar os checkboxes dos públicos atendidos*/
        if(value['codigo_publico']) {
            for(i = 1; i <= 9; i++) {
                if(value['codigo_publico'] == i) {
                    $('#pub'+ i).prop('checked', true);
                    if(value['codigo_publico'] == 9) {
                        $('input[name=txtOutrosPublicos]').val(value['outros_publicos']);
                    }
                }
            }
        }
    }
  • What gives alert(typeof value['outras_atividades']);?

  • Have you thought about adding a variable, then if calling the variable? value = value['codigo_activity']

  • @Vanderson I’ve done it to make a test, and the result was the same.

  • Have you tried jQuery.proxy?? http://blog.dmatoso.com/2011/05/howto function/

  • @Allanramos gave undefined before the if? or be right after var i = 0;?

  • @Sergio did Alert right after var i = 0; and he gave string, and inside if gave Undefined

  • Okay. That mistake doesn’t make much sense to me, the if doesn’t change anything. The question code is exactly the same as what you use? or is it a simplified version and the error may get lost in the way?

  • @Sergio the code is this exactly, I copied and pasted.

  • Puts console.log(JSON.stringify(value)); before var i = 0;. What returns?

  • It appeared like this: {"outras_activities":"Other Activities","sem_activities":"S"} E in the second line. {"activity code":11}

Show 5 more comments

1 answer

2


Your mistake is that you’re manipulating the msg.errorMsg.forEach(ShowResults);

msg.errorMsg.forEach will not pass to its function if it does not return error. You should just call the function ShowResults(value), containing the output of your json. This will manipulate on the side of the function that invoked the value passed.

errorMsg for the form with error. If there is no error in the position the message is undefined

Browser other questions tagged

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