assign variable with id dynamically via jquery

4

Hello What I need is the following: I have a form and in it I mount inputs whose values are the data of a table that I walk in a loop, I assign an id for each of these imputs with the field name and the number of an autoimcrement field in that table, hence I want I want to get the values of these input via jquery taking these values by the id of each input and for this I wanted to pass the id of each record and in jquery mount the name of the input id dynamically in a variable, something like:

function salvaCamposForm(idDaTabela){
   var idInput1 = '#nomedoimput1'+idDaTabela;
   var idInput2 = '#nomedoimput2'+idDaTabela;
   // dai, passar isso para atribuir o valor para a variavel que
   // que vai ser passada para o post
   fPostInput1 = $(idInput1).val();
   fPostInput2 = $(idInput2).val();
   // ...
}

Can this be done in JQuery?

Thank you!

Light and Peace !!!

    
asked by anonymous 01.05.2016 / 19:28

1 answer

4

Thank you @Sergio!

Your tip helped me a lot ...
I solved the probelma using a part of it, here is the solution:

    $(document).ready(function(){
       $('button').on('click', function() {
           var id = this.id;
           // this id button is btnSlvExperiencia_2
           var reference = id.substr(19,2);
           saveExperiencia(reference);
        });
     });

function saveExperiencia(ref) {
    fexpId              = ref;
    femail              = $('#xemail').val();
    fcex_empresa        = $('#_cex_empresa_'+ref).val();
    fcex_data_inicio    = $('#_cex_data_inicio_'+ref).val();
    fcex_data_fim       = $('#_cex_data_fim_'+ref).val();
    fcex_funcao         = $('#_cex_funcao_'+ref).val();
    fcex_endereco       = $('#_cex_endereco_'+ref).val();
    // ...

$.post("config/slvExpCorr.php",{
    experiencia_id: fexpId,
    email: femail,
    cex_empresa: fcex_empresa,
    cex_data_inicio: fcex_data_inicio,
    cex_data_fim: fcex_data_fim,
    cex_funcao: fcex_funcao,
    cex_endereco: fcex_endereco
    // ...
    },
    function(data) {
        if(data > 0) {
           $("#titCorr").html("<b>Informações Pessoais Alteradas com Sucesso!</b>");
        } else {
            $("#titCorr").html("<b>Erro ao atualizar suas Informações Pessoais, verifique!</b>");
            $("#titCorr").css("color", "red");
        }
      }
    )
}

So, with this, I consider the issue solved worked round

Light and Peace!

    
02.05.2016 / 21:06