How to select a radio button, according to the server response, jquery?

1

Good morning. Currently I fill a table dynamically and each record with its radio button

    var donaciones = $('.donaciones');
                            $.each(donativos, function(index,value){
                                $('<tr>')
                                    .append($('<td/>').addClass('nuevo-td descripcion').text(value.Descripcion))
                                    .append('<div class="tipo" style="display:none">' + value.Tipo + '</div>')
                                    .append('<div class="codigoinventario" style="display:none">' + value.Codigo_Inventario + '</div>')
                                    .append('<div class="cantidad" style="display:none">' + value.Cantidad + '</div>')
                                    .append('<div class="tipo_costo" style="display:none">' + value.TipoCosto + '</div>')
                                    .append('<div class="precio_socio" style="display:none">' + value.PrecioSocio + '</div>')
.append('<div class="tipo_seleccion" style="display:none">' + value.tipo_seleccion+ '</div>')
                                    .append('<div class="volumen_negocio" style="display:none">' + value.VolumenNegocio + '</div>')
                                    .append($('<td/>').addClass('label-cell nuevo-td classCantidad')
                                        .append($('<label/>').addClass('label-radio item-content')
                                                    .append('<input type="radio" name="donacion" class="donacion" attr-num-doc="' + value.Codigo_Inventario + '" value="' + value.Codigo_Inventario + '"/>')
                                                    .append($('<span/>').addClass('item-media').append('<i class="icon icon-form-radio"></i>'))))
                                    .appendTo(donaciones);
                            });

In these registers there is a property tipo_seleccion that can have the value 1 or 0, it turns out that the radio button that has tipo_seleccion = 1 must be marked on the radio button. Visually the table is like this:

    
asked by JG_GJ 29.10.2018 в 16:12
source

1 answer

2

You only need to add a control flag var activo = value.tipo_seleccion==1 ? 'checked="checked"' : ''; and then in the radio input you concatenate that control variable.

<script type="text/javascript">
var donaciones = $('.donaciones');
$.each(donativos, function(index,value){
var activo = value.tipo_seleccion==1 ? 'checked="checked"' : '';
$('<tr>')
    .append($('<td/>').addClass('nuevo-td descripcion').text(value.Descripcion))
        .append('<div class="tipo" style="display:none">' + value.Tipo + '</div>')
        .append('<div class="codigoinventario" style="display:none">' + value.Codigo_Inventario + '</div>')
        .append('<div class="cantidad" style="display:none">' + value.Cantidad + '</div>')
        .append('<div class="tipo_costo" style="display:none">' + value.TipoCosto + '</div>')
        .append('<div class="precio_socio" style="display:none">' + value.PrecioSocio + '</div>')
        .append('<div class="tipo_seleccion" style="display:none">' + value.tipo_seleccion+ '</div>')
        .append('<div class="volumen_negocio" style="display:none">' + value.VolumenNegocio + '</div>')
        .append($('<td/>').addClass('label-cell nuevo-td classCantidad')
            .append($('<label/>').addClass('label-radio item-content')
                .append('<input type="radio" '+activo+' name="donacion" class="donacion" attr-num-doc="' + value.Codigo_Inventario + '" value="' + value.Codigo_Inventario + '"/>')
                .append($('<span/>').addClass('item-media').append('<i class="icon icon-form-radio"></i>'))))
                    .appendTo(donaciones);
                    });
</script>
    
answered by 29.10.2018 / 16:17
source