javascript - 找不到 JavaScript [object HTMLSpanElement],我该如何解决
问题描述
"use strict";
let btn = document.getElementById("buttonCarrito");
btn.addEventListener("click",agregar);
//this function is agregar (add in English), the idea is that this function put the elements Manzana (Apple) in the shop cart but when i try do this i get [object HTMLSpanElement]50 (50 is the Apple value, but i don't can show only the number 50)
function agregar(){
let Productos= {
"Manzana": "50",
"Banana": "40",
"Naranja": "30",
"Mandarina": "20"
}
console.table(Productos)
let frutaComprada= document.getElementById("inputProducto").value;
let costoTotal= document.getElementById("valor");
let productoSeleccionado=Productos[frutaComprada];
costoTotal=costoTotal+productoSeleccionado;
valor.innerHTML=costoTotal;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div class="container">
<h1>Bienvenido a la tienda</h1>
<input id="inputProducto" type="text" placeholder="Ingrese su producto">
<br>
<br>
<input id="inputCompra" type="text" placeholder="Ingrese el valor de su compra">
<br>
<br>
<button id="buttonCarrito">Agregar al carrito</button>
<p>El valor total de su compra es: <span id="valor"> 0 </span></p>
</div>
</body>
<script src="js.js"></script>
</html>
解决方案
来自表单输入的值将始终是字符串,因此要将数字相加,您需要将字符串强制转换为数字。几种方法: 1)
Number(str)
或 2)+str
。您的产品/价格对象:无需将这些价格作为字符串。
您遇到的主要问题
let costoTotal= document.getElementById("valor");
是只拾取元素而不是文本内容。我们可以使用let costoTotal= document.getElementById("valor").textContent;
它,但是我们需要将它强制转换为类似于输入值的数字。
(旁注:目前您的代码没有使用inputCompra
我在评论中询问它的原因。因此,例如,无论在该输入中输入什么,香蕉的总数都会增加 40。)
let btn = document.getElementById("buttonCarrito");
btn.addEventListener("click", agregar);
function agregar() {
let Productos = {
Manzana: 50,
Banana: 40,
Naranja: 30,
Mandarina: 20
}
let frutaComprada = document.getElementById("inputProducto").value;
let costoTotal = Number(document.getElementById("valor").textContent);
let productoSeleccionado = Productos[frutaComprada];
costoTotal = costoTotal + productoSeleccionado;
valor.textContent = costoTotal;
}
<div class="container">
<h1>Bienvenido a la tienda</h1>
<input id="inputProducto" type="text" placeholder="Ingrese su producto">
<br>
<br>
<input id="inputCompra" type="text" placeholder="Ingrese el número de artículos">
<br>
<br>
<button id="buttonCarrito">Agregar al carrito</button>
<p>El valor total de su compra es: <span id="valor"> 0 </span></p>
</div>
这是一个使用数量的版本,以防您好奇。
let Productos = {
Manzana: 50,
Banana: 40,
Naranja: 30,
Mandarina: 20
}
const product = document.getElementById('inputProducto');
const quantity = document.getElementById('inputCompra')
const valor = document.getElementById('valor');
const btn = document.getElementById('buttonCarrito');
btn.addEventListener('click', agregar);
function agregar() {
const frutaComprada = product.value;
const itemQuantity = Number(quantity.value);
const productoSeleccionado = Productos[frutaComprada];
const subTotal = productoSeleccionado * itemQuantity;
let costoTotal = Number(valor.textContent);
costoTotal = costoTotal + subTotal;
valor.textContent = costoTotal;
}
<div class="container">
<h1>Bienvenido a la tienda</h1>
<input id="inputProducto" type="text" placeholder="Ingrese su producto">
<br>
<br>
<input id="inputCompra" type="text" placeholder="Ingrese el valor de su compra">
<br>
<br>
<button id="buttonCarrito">Agregar al carrito</button>
<p>El valor total de su compra es: <span id="valor"> 0 </span></p>
</div>
推荐阅读
- javascript - 表单“重置”事件不会冒泡哪些浏览器?
- python - 在 pyspark 中使用 Split() 函数
- r - 不显示数字是 y 轴
- google-analytics - 谷歌分析中的展示次数为零,而会话和用户更多
- python - ValueError: Input 0 of layer dense_8 is in compatible with the layer: using tf.keras 2 CNNs to concatenate
- c# - XtraScheduler 未显示新约会
- excel - 为什么我的代码会出现运行时错误 91?
- javascript - 使用用户提交的信息向表中添加一行 (HTML/JS)
- javascript - React/Redux 无法读取未定义的“prop”
- javascript - 更新不是函数-> firebase/firestore 写入数据