gpt4 book ai didi

javascript - 引用错误: "function" is not defined

转载 作者:行者123 更新时间:2023-12-03 11:05:18 27 4
gpt4 key购买 nike

我在购物篮添加到购物车代码中收到错误“ReferenceError:AddtoCart 未定义”。该按钮是一次性执行的。我的 JavaScript 是:

        <script type="text/javascript">
var basket = [];

// display basket and fill cells
function displaybasket() {
var shoppingBasketTblBody = document.getElementById("shoppingBasketTblBody");
while (shoppingBasketTblBody.rows.length > 0) {
shoppingBasketTblBody.deleteRow(0);
}

var basket_total = 0.00;

//populating the table

for (var product in basket) {
var removeButton = document.createElement('input');
removeButton.type = 'button';
removeButton.value = 'X';
removeButton.onclick = removeItem;
/* could not work out how to implement without using cookies, ran out of time.
var addButton = document.createElement('input');
addButton.type = 'button';
addButton.value = '+';
addButton.onclick = 'addItem';
var minusButton = document.createElement('input');
minusButton.type = 'button';
minusButton.value = '-';
minusButton.onclick = 'minusItem;' */
var row = shoppingBasketTblBody.insertRow();
var cellName = row.insertCell(0);
var cellDescription = row.insertCell(1);
var cellPrice = row.insertCell(2);
var cellAmount = row.insertCell(3);
var cellRemove = row.insertCell(4);
cellPrice.align = "right";
cellName.innerHTML = basket[product].Name;
cellDescription.innerHTML = basket[product].Description;
cellPrice.innerHTML = "£" + basket[product].Price.toFixed(2);
cellAmount.innerHTML = basket[product].Quantity;
cellRemove.appendChild(removeButton);
basket_total += parseFloat(basket[product].Price);
console.log(basket_total);
}
// display total cost
document.getElementById("cart_total").innerHTML = "£" + basket_total.toFixed(2);
}


function AddtoCart(name, Description, price, Quantity) {
//create product
var item = {};
item.Name = name;
item.Description = Description;
item.Price = parseFloat(price);
item.Quantity = Quantity;
//push to basket
basket.push(item);
//call display basket function
displaybasket();
}

function removeAll() {
basket.length = 0.0;
basket_total = 0.00;
document.getElementById("shoppingBasketTblBody").deleteRow();
}

function removeItem(i) {
foodList.splice(i, 1); // remove element at position i
var newFood = "";
for (var i = 0; i & lt; foodList.length; i++) {
newFood += "<a href='#' onClick='removeRecord(" + i + ");'>X</a> " + foodList[i] + " <br>";
};
document.getElementById('foods').innerHTML = newFood;
}

/*could not work out how to implement without using cookies, ran out of time.
function addItem() {

}

function minusItem() {

}*/

按钮代码是:

 <button type="button" onclick="AddtoCart('{Product}','{Description}','{price}','1')">Add one to cart</button>

产品、描述、价格从 XML 传递。

最佳答案

removeItem 函数中的 for 循环无效。 AddtoCart 在以下代码片段中完美运行。

var basket = [];

// display basket and fill cells
function displaybasket() {
var shoppingBasketTblBody = document.getElementById("shoppingBasketTblBody");
while (shoppingBasketTblBody.rows.length > 0) {
shoppingBasketTblBody.deleteRow(0);
}

var basket_total = 0.00;

//populating the table

for (var product in basket) {
var removeButton = document.createElement('input');
removeButton.type = 'button';
removeButton.value = 'X';
removeButton.onclick = removeItem;
/* could not work out how to implement without using cookies, ran out of time.
var addButton = document.createElement('input');
addButton.type = 'button';
addButton.value = '+';
addButton.onclick = 'addItem';
var minusButton = document.createElement('input');
minusButton.type = 'button';
minusButton.value = '-';
minusButton.onclick = 'minusItem;' */
var row = shoppingBasketTblBody.insertRow();
var cellName = row.insertCell(0);
var cellDescription = row.insertCell(1);
var cellPrice = row.insertCell(2);
var cellAmount = row.insertCell(3);
var cellRemove = row.insertCell(4);
cellPrice.align = "right";
cellName.innerHTML = basket[product].Name;
cellDescription.innerHTML = basket[product].Description;
cellPrice.innerHTML = "£" + basket[product].Price.toFixed(2);
cellAmount.innerHTML = basket[product].Quantity;
cellRemove.appendChild(removeButton);
basket_total += parseFloat(basket[product].Price);
console.log(basket_total);
}
// display total cost
document.getElementById("cart_total").innerHTML = "£" + basket_total.toFixed(2);
}


function AddtoCart(name, Description, price, Quantity) {
//create product
var item = {};
item.Name = name;
item.Description = Description;
item.Price = parseFloat(price);
item.Quantity = Quantity;
//push to basket
basket.push(item);
//call display basket function
displaybasket();
}

function removeAll() {
basket.length = 0.0;
basket_total = 0.00;
document.getElementById("shoppingBasketTblBody").deleteRow();
}

function removeItem(i) {
foodList.splice(i, 1); // remove element at position i
var newFood = "";
//for (var i = 0; i & lt; foodList.length; i++) {
// newFood += "<a href='#' onClick='removeRecord(" + i + ");'>X</a> " + foodList[i] + " <br>";
//};
document.getElementById('foods').innerHTML = newFood;
}

/*could not work out how to implement without using cookies, ran out of time.
function addItem() {

}

function minusItem() {

}*/
<button type="button" onclick="AddtoCart('{Product}','{Description}','{price}','1')">Add one to cart</button>

<table id="shoppingBasketTblBody">
</table>

<p id="demo"></p>
<p id="cart_total"></p>

关于javascript - 引用错误: "function" is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27888470/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com