Thousands separators

1

I have the following structure.

function agregarDetalle(idarticulo,nombre)
  {
    var cantidad=1;
    var precio_compra=1;
    var precio_venta=1;
    if (idarticulo!="")
    {
        var subtotal=cantidad*precio_compra;
        var fila='<tr class="filas" id="fila'+cont+'">'+
        '<td><button type="button" class="btn btn-danger" onclick="eliminarDetalle('+cont+')">X</button></td>'+
        '<td><input type="hidden" name="idarticulo[]" value="'+idarticulo+'">'+nombre+'</td>'+
        '<td><input type="number" name="cantidad[]" id="cantidad[]" value="'+cantidad+'"></td>'+
        '<td><input type="number" name="precio_compra[]" id="precio_compra[]" value="'+precio_compra+'"></td>'+
        '<td><input type="number" name="precio_venta[]" value="'+precio_venta+'"></td>'+
        '<td><span name="subtotal" id="subtotal'+cont+'">'+subtotal+'</span></td>'+
        '<td><button type="button" onclick="modificarSubototales()" class="btn btn-info"><i class="fa fa-refresh"></i></button></td>'+
        '</tr>';
        cont++;
        detalles=detalles+1;
        $('#detalles').append(fila);
        modificarSubototales();
    }
    else
    {
        alert("Error al ingresar el detalle, revisar los datos del artículo");
    }
  }

to which I need to add separators of thousands to the input with id #precio_buy and #precio_venta

probe with the examples: Thousands separator with javascript php or Jquery and < a href="https://es.stackoverflow.com/questions/128303/como-dar-formato-de-n%C3%BAmero-a-todos -los-elementos"> How to give a number format to all the elements?

but none of them works for me.

    
asked by JohnL 03.02.2018 в 17:52
source

1 answer

1

If you want to apply a custom format to the numbers you can not use type="number" . In this way, it is the browser that controls the format for entering numbers.

You should change them by type="text" and then control by code that the values entered by the user are correct.

var cont= 1;

function agregarDetalle(idarticulo,nombre)
{
  var cantidad = parseInt(Math.random() * 100);
  var precio_compra = Math.random() * 20;
  var precio_venta = Math.random() * 35;
  if (idarticulo!="")
  {
    var subtotal = cantidad * precio_compra;
    var fila='<tr class="filas" id="fila'+cont+'">'+
    '<td><button type="button" class="btn btn-danger" onclick="eliminarDetalle('+cont+')">X</button></td>'+
    '<td><input type="hidden" name="idarticulo[]" value="'+idarticulo+'">'+nombre+'</td>'+
    '<td><input type="number" name="cantidad[]" id="cantidad[]" value="' + cantidad +'"></td>'+
    '<td><input type="text" name="precio_compra[]" id="precio_compra[]" value="'+precio_compra.toLocaleString() +'"></td>'+
    '<td><input type="text" name="precio_venta[]" value="'+precio_venta.toLocaleString()+'"></td>'+
    '<td><span name="subtotal" id="subtotal'+cont+'">'+subtotal.toLocaleString()+'</span></td>'+
    '<td><button type="button" onclick="modificarSubototales()" class="btn btn-info"><i class="fa fa-refresh"></i></button></td>'+
    '</tr>';
    cont++;
    detalles=detalles+1;
    $('#detalles').append(fila);
    //modificarSubototales();
  }
  else
  {
    alert("Error al ingresar el detalle, revisar los datos del artículo");
  }
}
  
$(function(){
  $('#agregar').click(function(){
    agregarDetalle('ID' + cont, 'Artículo ' + cont);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="detalles"></table>

<button id="agregar">Agregar</button>
    
answered by 03.02.2018 / 18:11
source