Do not load the actual values I choose from a select laravel - js

0

I have a select that when selecting I bring some information from the database by means of an ajax, so everything is normal, and I have the following function in javascript that executes certain operations with that data based on the selection I made previously,

JS:

 var probar = function(){

        var id_maquina = this.value;
        var nu_cylinders = document.getElementById('nu_cylinders');
        var nu_perimeter_cylinder_a_a = document.getElementById('nu_perimeter_cylinder_a_a');
        var nu_width_cylinder_l_l = document.getElementById('nu_width_cylinder_l_l');
        var url = "maquinas/"+id_maquina;

        $.get(url, function(data){
            nu_cylinders.value = data.nu_cylinders;
            nu_perimeter_cylinder_a_a.value = data.nu_perimeter_cylinder_a_a;
            nu_width_cylinder_l_l.value = data.nu_width_cylinder_l_l;
        });

        var perimetro_a_a = nu_perimeter_cylinder_a_a.value;
        var ancho_cilindro_l_l = nu_width_cylinder_l_l.value;
        var ancho_bobina_l_l = $('#bobbin_Width_l_l').val();
        var dist_fotocelda = $('#photocell_width_a_a').val();
        var repeticiones_l_l = $('#number_repetitions_l_l').val();
        var refile_mp = $('#refile_mp').val();

        console.log(perimetro_a_a,ancho_cilindro_l_l,ancho_bobina_l_l,dist_fotocelda);  

        if(perimetro_a_a<ancho_bobina_l_l){
            alert('Error: El perimetro A-A´ es inferior al ancho de bobina requerido, favor seleccione otra maquina');
            $('#nu_perimeter_cylinder_a_a').addClass("uk-form-danger");
            $('#nu_perimeter_cylinder_a_a').removeClass("uk-form-success");
        } else {
            $('#nu_perimeter_cylinder_a_a').removeClass("uk-form-danger");
            $('#nu_perimeter_cylinder_a_a').addClass("uk-form-success");
            var operacion = Math.round(perimetro_a_a/ancho_bobina_l_l);
            var diferencia = (ancho_bobina_l_l-(perimetro_a_a/operacion)).toFixed(2);
            var repeticiones = Math.floor(perimetro_a_a/ancho_bobina_l_l);
            console.log(operacion,diferencia,repeticiones);
            difference.value = diferencia;
            number_repetitions_l_l_sug.value = repeticiones;
            }

    };

    $('#convert_machine').change(probar);

Every time I select a new option of the select in the view, the value brought from the database is correctly seen, but the calculations are done with values prior to the selection I made.

image:

In the image it is seen that I select an option that shows me the perimeter with a value of 650 but in the console it tells me that it is operating with a perimeter value of 680, but when I select another option of the select there, if it brings me in the console the perimeter value of 650 but in the view you see a completely different one and so on. I think the error is in the function stored in the test variable but I do not know what exactly it is, what could be done in this particular case?

    
asked by Darwin Gomez 04.12.2017 в 08:01
source

1 answer

0

The new value is selected when the option changes, not when you click. Therefore:

$(function(){

        $('#convert_machine').change(function(){

                var id_maquina = this.value;
                var nu_cylinders = document.getElementById('nu_cylinders');
                var nu_perimeter_cylinder_a_a = document.getElementById('nu_perimeter_cylinder_a_a');
                var nu_width_cylinder_l_l = document.getElementById('nu_width_cylinder_l_l');
                var url = "maquinas/"+id_maquina;

                $.get(url, function(data){
                    nu_cylinders.value = data.nu_cylinders;
                    nu_perimeter_cylinder_a_a.value = data.nu_perimeter_cylinder_a_a;
                    nu_width_cylinder_l_l.value = data.nu_width_cylinder_l_l;
                });

        });

     });
    
answered by 04.12.2017 в 10:58