【发布时间】:2022-01-12 04:57:39
【问题描述】:
Value1.length 正在发送一个未定义的错误,但其他两个值不是。为什么只有第一个值没有正常运行?
placeOrder.addEventListener("click", function (e) {
e.preventDefault();
var value1 = input1.value;
if (value1.length < 1) {
value1 = 0;
coffeeTotal = 0;
} else {
coffeeTotal = coffeePrice * parseInt(value1);
}
var value2 = input2.value;
if (value2.length < 1) {
value2 = 0;
bagelTotal = 0;
} else {
bagelTotal = bagelPrice * parseInt(value2);
}
var value3 = input3.value;
if (value3.length < 1) {
value3 = 0;
biscottiTotal = 0;
} else {
biscottiTotal = biscottiPrice * parseInt(value3);
}
var total = coffeeTotal + bagelTotal + biscottiTotal;
console.log(total);
});
【问题讨论】:
-
什么是
input1, input2, input3? -
input1 的值在哪里分配,它应该是什么?在
coffeePrice * parseInt(value1)中使用 parseInt 是多余的,因为乘法无论如何都会强制参数类型为 number。 -
var input1 = document.getElementById("coffee"); var input2 = document.getElementById("bagel"); var input3 = document.getElementById("biscotti");
-
请先添加
console.log(document.getElementById("coffee"));,看看能不能找到那个DOM元素
标签: javascript dom