get jquery autocomplete value for dynamic online calculation

0

I need to get the value returned from autocomplete to make a sum in a dynamic list of products, since it already calculates value and quantity and now I have to insert a new column "MEDIDA" and it is necessary to remove the letters and pick only to the measure qtd, follow the code:

  

autocomplete

// MEDIDA

var x_medida = [

   "1 UNIDADE",
    "PCT 10",
    "PCT 20",
    "PCT 30",
    "PCT 40",
    "PCT 50"

];

$( ".medida" ).autocomplete({ source: x_medida });
  

Dynamic table

contador++;


var newRow = $("<tr>");
var cols = "";

cols += '<td class="contador valor_total" >' + contador + '</td>';
cols += '<td><label text-align="center"><input type="text" name="produto' + contador + '" class="produto" /></label></td>';
cols += '<td><label text-align="center"><input type="text" name="medida'  + contador + '" class="medida centro" /></label></td>';
cols += '<td><label text-align="center"><input type="text" name="qtd'     + contador + '" class="qtd centro" onkeyup="somenteNumeros(this);" /></label></td>';
cols += '<td><label text-align="center"><input type="text" name="preco'   + contador + '" class="preco centro"  align="center" /></label></td>';
cols += '<td class="col-md-2 centro"><b> R$ 0.00 </b></td>';
cols += '<td><a class="deleteLinha"> Excluir </a></td>';

newRow.append(cols);

$("#products-table").append(newRow);
  

sum of line values

// FUNCTION ( CALCULA TOTAL DA LINHA ) 

function calculateRow(row) {

    var preco    = +row.find('input[name^="preco"]').val();
    var qtd      = +row.find('input[name^="qtd"]').val();
    //var medida   = +row.find('input[name^="medida"]').val();

    //2 casas decimais
    var total = (preco * qtd).toFixed(2);

    //substitui ponto por virgula
    total = total.replace(".", ",");

    //a regex abaixo coloca um ponto a esquerda de cada grupo de 3 digitos desde que não seja no inicio do numero
    row.find('.total').html("R$ " + (total).replace(/\B(?=(\d{3})+(?!\d))/g, "."));     

}
    
asked by anonymous 29.03.2018 / 23:11

1 answer

1

You can use match with the regex below to get only the value numbers:

var medida   = +row.find('input[name^="medida"]').val().match(/\d+/);

If the value is: "PCT 10" , it will return "10" . And you can use this value to include in your calculations.

    
30.03.2018 / 02:52