Freight Calculation Ajax and Jquery

0

I am developing an online store where in the shopping cart I will implement the freight calculation via the webservice of the post office.

The code below it does the calculation and returns only the option of Pac, I need that as the user select the sedex option he make the calculation and show me the value referring to sedex. I am not able to implement select, it is appearing that the var type freight is not defined.

function LoadFrete() {

var cep_destino = $('#cep_destino').val();
//var tipo_frete = $('tipo_frete').val();



$.ajax({
    url: 'ajax/a_frete.php',
    type: 'POST',
    dataType: 'html',
    cache: false,
    data: {cep_destino: cep_destino},
    success: function (data) {

        console.log('Valor = ' + data);

        $('#valor_frete').val(data);

        var val_prod = $('#valor_pro').val();

        val_prod = val_prod.replace(',', '.');
        data     = data.replace(',', '.');

        var total = parseFloat(data) + parseFloat(val_prod);

        $('#valor_prodfrete').val(total);



    }, beforeSend: function () {

    }, error: function (jqXHR, textStatus, errorThrown) {
        console.log('Erro');

    }
});
}   
    
asked by anonymous 27.08.2017 / 07:25

1 answer

0

Depends on what you are using in your form, whether it is class or id

Example:

$('.btn-calcular').on('click', function(){
   var tipo_frete_class = $('.tipo_frete').val();
   var tipo_frete_id = $('#tipo_frete').val();
   
   alert("Tipo Frete  class: "+tipo_frete_class+
        "\nTipo de Frete id: "+tipo_frete_id);
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-3.2.1.js"></script><labelfor="tipo_frete">Tipo Frete Class</label>
<div class="col-lg-4">
<select class="form-control tipo_frete col-lg-2" >
  <option value="1">Normal</option>
  <option value="2">Sedex</option>
</select>

<br><br>

<label for="tipo_frete">Tipo Frete ID</label>
<select class="form-control  col-lg-2" id="tipo_frete">
  <option value="1">Normal</option>
  <option value="2">Sedex</option>
</select>
</div>

<button class="btn btn-primary btn-calcular">Calcular</button>

And in the dataType, try using json

dataType: 'json'
    
27.08.2017 / 18:23