helps to capture data with $ (this) .val ();

1

I have this code.

$.post("tipo_documentos/llenar_combo",{accion:''},function(data){
    $("#documento").append(new Option('Seleccione',''));
    for (i = 0; i < data.length; i++) {
        $("#documento").append(new Option(data[i][1],data[i][0],data[i][6]));

    };
    //$('#documento option[value="6').prop("selected", true);
},'json');

as you will see in new option I have 3 data data[i][1] which is the name. data[i][0] which is the code and data[i][6] which is the type_document

Now I have this other code where I capture the data and show it in inputs:

$("#documento").on('change', function() {
    if ($(this).val()!="") {

        var codigo = $(this).val();                   
        var nombre = $("#documento option:selected").text();

        if (codigo == '07'|| codigo == '08')
          {
          $("#note").show();
          } else {
          $("#note").hide();
          }                  
        $("#nomb").val(nombre);           
        $("#cod").val(codigo);
        $.post("ventas/datos_documento" , {Codigo:codigo,Nombre:nombre} , function(data){
            $("#serie").val(data.Serie);
            $("#numero").val(data.Numero);             

        },'json');
    }else{
        $("#serie").val('');
        $("#numero").val('');                   
    }
});

the var code captures the code and the var names the name but how do I capture the document type?

    
asked by Fernando Abel Gonzales Ch 09.02.2018 в 16:44
source

1 answer

1

As I said in the comments new Option () alone you will not add a "third" attribute to your <option> , so you must add it manually using the .attr() method jQuery .

I leave you a small functional example, if you have any questions you can ask:

$("#documento").append(new Option('Seleccione',''));
for (i = 0; i < 6; i++) {
    $("#documento").append($(new Option('Opción ' + i, i)).attr('tipo', 'tipo ' + i));
};

$("#documento").on('change', function() {
    if ($(this).val()!="") {
        var codigo = $(this).val();                   
        var nombre = $("#documento option:selected").text();
        var tipo = $("#documento option:selected").attr('tipo');
        
        console.log('El código es: ' + codigo + '\nEl nombre es: ' + nombre + '\nEl tipo es: ' + tipo);

    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<select id="documento"></select>

Using your code would be as follows:

$.post("tipo_documentos/llenar_combo",{accion:''},function(data){
    $("#documento").append(new Option('Seleccione',''));
    for (i = 0; i < data.length; i++) {
        $("#documento").append($(new Option(data[i][1],data[i][0])).attr('tipo', data[i][6]));

    };
    //$('#documento option[value="6').prop("selected", true);
},'json');

$("#documento").on('change', function() {
    if ($(this).val()!="") {

        var codigo = $(this).val();                   
        var nombre = $("#documento option:selected").text();
        var tipo = $("#documento option:selected").attr('tipo');

        if (codigo == '07'|| codigo == '08')
          {
          $("#note").show();
          } else {
          $("#note").hide();
          }                  
        $("#nomb").val(nombre);           
        $("#cod").val(codigo);
        $.post("ventas/datos_documento" , {Codigo:codigo,Nombre:nombre} , function(data){
            $("#serie").val(data.Serie);
            $("#numero").val(data.Numero);             

        },'json');
    }else{
        $("#serie").val('');
        $("#numero").val('');                   
    }
});
    
answered by 09.02.2018 / 17:32
source