String and variable concatenation problem in jquery load function

Asked

Viewed 1,821 times

1

Follow the full Javascript code:

var idcl = document.getElementById('idcl').value;
$(document).ready(function(){
    $('#estados').change(function(){
        $('#cidades').load('cidades.php?estado='+$('#estados').val()+'&cliente='+idcl);
        $('#transacao').load('transacao.php?estado='+$('#estados').val()+'&cliente='+idcl+'&teste='+$('#cidades').val());
    });
});

Select fields:

 <select name="cidades" id="cidades">...</select>
 <select name="transacao" id="transacao">...</select>

I’m not getting to make this parameter: teste='+$('#cidades').val() receive the value you are in #cidades.

4 answers

2

Try this using the jQuery method even, below I created a JSON for each parameter:

$(document).ready(function(){
    $('#estados').change(function(){
       var params = {
            cliente:$('#idcl').val(),
            cidades:$('#cidades').val(),
            estado:$('#estados').val()
       };
        $('#cidades').load('cidades.php?estado=' + params.estado + '&cliente=' + params.cliente);
        $('#transacao').load('transacao.php?estado=' + params.estado + '&cliente=' + params.cliente + '&teste=' + params.cidades);
    });
});

2

Try to separate the value into a variable.

var idcl = document.getElementById('idcl').value;
$(document).ready(function(){
    $('#estados').change(function(){
        var estados = $('#estados').val();
        $('#cidades').load('cidades.php?estado='+estados+'&cliente='+idcl);
        $('#transacao').load('transacao.php?estado='+ estados +'&cliente='+idcl+'&teste='+$('#cidades').val());
    });
});
  • I understood but how will I capture the values I am in #states and #cities ?? Which code I use?

  • I changed the code, take a look, see if concatenou the state, do the same with city.

1

Man, the problem must be us option's his select.

Make sure you’ve written your select as <option value='ba'>Bahia</option>. In the case I mentioned, if you use $('#estados').val(), he will return "ba".

Also check that the id is correct in the select.

Sometimes it’s a typing problem that goes unnoticed.

1

To redeem values from <select> in jQuery, it is done so:

var estado = $('#estados option:selected').attr('value');

For the object <select> has no value, it has several options, and you have to bring the value of the selected option.

Browser other questions tagged

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