[Ayuda] Reemplazar elemento de la cesta y calcular el precio total
Publicado por TrustMaster (6 intervenciones) el 05/11/2019 22:12:19
Buenas noches, estaba intentando realizar un procedimiento a través de un script en el que me gustaría poder hacer unos arreglos, entre ellos que al añadir productos no se añada uno tras otro sino que este se sustituya y aumente la cantidad de productos y por otro lado con un botón que al hacer click me genere una funcion en donde me calcule el precio total de toda la cesta, pero no consigo obtener un buen resultado.
Os adjunto el html y el js para ver si me podéis echar una mano:
Os adjunto el html y el js para ver si me podéis echar una mano:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
var arrayProductos;
var cantidad=1;
var arrayCesta=[];
function inicio(){
producto();
}
function Producto(idProducto, nombreProducto, cantidad, precio){
this.idProducto= idProducto;
this.nombreProducto = nombreProducto;
this.cantidad = cantidad;
this.precio = precio;
}
function producto(){
var placa = new Producto("0", "Placa base Gigabyte GA-H110M-S2H", 1,"42,14");
var ssd = new Producto("1","Disco duro Kingston A-400 SSD 480GB", 1,"45,45");
var ram = new Producto("2","Memoria RAM DDR4 Corsair Vengeance LPX 3000 Mhz", 1,"69,12");
arrayProductos = [placa, ssd, ram];
for (i = 0; i < arrayProductos.length; i++) {
document.getElementById("tablaProductos").innerHTML+="<tr><td>"+arrayProductos[i].nombreProducto+"</td> <td>"+arrayProductos[i].cantidad+"</td> <td>"+arrayProductos[i].precio+"</td><td><input type='button' onclick='anadir("+i+")' size='50' name='btnAnadir'></input></td></tr>";
}
}
function anadir(i){
arrayCesta.push(i);
acumulador="<tr><td>Producto</td><td>Cantidad cesta</td><td>Precio unitario</td><td>Precio total</td><td></td></tr>";
for (var p = 0; p < arrayCesta.length; p++) {
document.getElementById("cesta").innerHTML+=arrayCesta[p];
acumulador+="<tr id='lin_"+p+"'><td>"+arrayProductos[arrayCesta[p]].nombreProducto+"</td> <td id='cantidad'>"+arrayProductos[arrayCesta[p]].cantidad+"</td> <td>"+arrayProductos[arrayCesta[p]].precio+"</td><td><input type='button' onclick='quitarDeCesta("+p+");' size='50' value='Eliminar'></td></tr>";
}
document.getElementById("cesta").innerHTML=acumulador;
}
function quitarDeCesta(i){
arrayCesta.splice(i, 1);
var quita_p_cesta= document.getElementById("lin_"+i);
quita_p_cesta.parentNode.removeChild(quita_p_cesta);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<html>
<head>
<title></title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<script type="text/javascript" src="javascript.js"></script>
<link rel="stylesheet" href="estilos.css" type="text/css" title="actual" />
</head>
<body onload="inicio()">
<div id="products" class="productos">
<table id="tablaProductos" class="tabla">
<tr>
<td>Producto</td>
<td>Cantidad</td>
<td>Precio</td>
</tr>
</table>
<table align="center" border="2" id="cesta" class="cesta">
<tr>
<td>Producto</td>
<td>Cantidad cesta</td>
<td>Precio unitario</td>
<td>Precio total</td>
</tr>
</table>
</div>
</body>
</html>
Valora esta pregunta


0