Select with Json

0

Hello, how are you? I'm trying to make a select popular with Json that has already selected the item. I was able to make up the population of the select listing all the models of only cars I could not already bring it selected. Can someone help me?

var handlerCarregaMarca = function(){

        var marca = $('#tipo-id').val();
        var marcaid = $('#marca-id').val();

        console.log(marca);
        console.log(marcaid);


        $.ajax({
            type: "GET",
            url:'listarMarcas?codigo='+marca,
            dataType: "html",
            async:false,
            success: function (data) {
                // console.log(data);

                var output= '<option value="">Selecione o Tipo do Veiculo</option>';
                var dados = JSON.parse(data);

                $.each(dados, function(i, item){
                    output += '<option value="'+item.marc_id+'""'+item.marc_id == marcaid ? 'selected':''+'">'+item.marc_nome+'</option>'
                });

                $("#marca-veiculo").html(output);
                $("#marca-veiculo").trigger('chosen:updated');
            }
        });

        $(this).val(marca);

This is the code I'm using.

    
asked by anonymous 11.05.2018 / 20:41

1 answer

0

face has an error in its logical operation.

try changing this:

$.each(dados, function(i, item){
  output += '<option value="'+item.marc_id+'""'+item.marc_id == marcaid ? 'selected':''+'">'+item.marc_nome+'</option>'
});

so:

$.each(dados, function(i, item){
  output += "<option value='"+item.marc_id+"' "+((item.marc_id == marcaid) ? 'selected':'')+">"+item.marc_nome+"</option>"
});
    
11.05.2018 / 21:26