Necesito aplicar 5% de descuento al valor de un input ingresado mediante un checkbox, pero dependiendo la posicion del input
Este es mi input donde pongo un precio
<input type="number" id="precio" name="precio[]" class="Precio" step='0.01'>
y este en el checkbox
<input id="{{$ticket->id}}"class=" Descuento" name="descuento[{{$j['sku']}}]" type="checkbox" value="si">
Lo que necesito es que cuando presione el checkbox segun la posicion del input precio, se le aplique el descuento fijo del 5%
Ejemplo: Si en mi primer input ingrese 100 y le doy al checbox se cambie a 95 que seria el resultado del precio -5%, y asi con cada posicion
este es mi script por ahora, donde realizo operaciones en tiempo real,
<script type="text/javascript">
function selects(id) {
var ele = document.getElementById(id);
for (var i = 0; i < ele.length; i++) {
if (ele[i].type == 'checkbox')
ele[i].checked = true;
}
}
let clase= '#Total';
$('#showtotal').hide();
function sumar() {
let total = 0;
$('.Precio').each(function(index) {
let cantidad = parseInt($('[name="totalProducto[]"]')[index].value) || 0;
let precio = parseFloat($(this).val()) || 0;
// let porcentaje=parseFloat(precio*0.95);
// $('.Descuento').change(function(){
// $('.Descuento').each(function(){
// if($('.Descuento')[index].checked==true){
// $('.Precio').val(porcentaje)[index];
// sumar();
// }
// });
// });
total += cantidad * precio
});
if(total > 0){
$('#showtotal').show();
}
else if(total == 0){
$('#showtotal').hide();
}
$(clase).val('$ '+total);
$('#totalSpan').text('$ '+total);
}
function descuento() {
let total = 0;
$('.Descuento').change(function(){
$('.Descuento').each(function(indexa){
if($('.Descuento')[indexa].checked==true){
let pre = parseInt($('[name="precio[]"]')[indexa].value);
let porcentaje=parseFloat(pre*0.95);
$('.Precio').val(porcentaje)[indexa];
sumar();
}
});
});
}
$( document ).ready(function() {
$('[name="totalProducto[]"]').on('input', sumar);
$('.Precio').on('input', sumar);
sumar();
descuento();
});
</script>