在Javascript中将变量重置为0

在Javascript中将变量重置为0,javascript,Javascript,我已经四处寻找了一段时间,想知道这个问题的答案 这是我的密码: function appendShopItem(shopItem, subTotal) { // Create the list item: var item = document.createElement( 'li' ); // Set its contents: item.appendChild( document.createTextNode( shopItem.name

我已经四处寻找了一段时间,想知道这个问题的答案

这是我的密码:

function appendShopItem(shopItem, subTotal)
{
    // Create the list item:
    var item = document.createElement( 'li' );

    // Set its contents:
    item.appendChild( document.createTextNode( 
        shopItem.name + ' - ' + shopItem.cost + ' Gold'
    ) );

    // Add it to the list:
    list.appendChild( item );

    var radio = document.createElement( 'button' );
    var text = document.createTextNode( "Buy " + shopItem.name + " for " + shopItem.cost + " Gold");
    radio.name = 'shop';
    radio.value = shopItem.name;
    radio.onclick = function () {
        subTotal += shopItem.cost;
        addValue( shopItem, subTotal );
    };

    radio.appendChild( text );
    document.body.appendChild( radio );

    var lineBreak= document.createElement("BR");
    document.body.appendChild(lineBreak);
}

function addValue(shopItem, subTotal)
{
    document.getElementById("extraSubHeader").innerHTML = "Current Total: " + subTotal + " Gold"
}

function enterShop(position, locationNames, locationDescriptions, buttons, buttonActions, shopItems, shopCosts)
{
    subTotal = 0;

    document.getElementById("extraHeader").style.visibility = "visible";
    document.getElementById("extraHeader").innerHTML = "Welcome to the Shop!";

    document.getElementById("extraSubHeader").style.visibility = "visible";
    document.getElementById("extraSubHeader").innerHTML = "Current Total: 0 Gold"

    document.getElementById("list").style.visibility = "visible";


    document.getElementById("option1").style.visibility = "hidden";
    document.getElementById("option2").style.visibility = "hidden";
    document.getElementById("optionalInfo").style.visibility = "hidden";
    document.getElementById("subHeader").style.visibility = "hidden";
    document.getElementById("actionButton").innerHTML = "Leave Shop";
    document.getElementById("actionButton").onclick = function (){ loadData(position, locationNames, locationDescriptions, buttons, buttonActions, shopItems, shopCosts); hideShop(); }
    document.getElementById("header").style.visibility = "hidden";

    for( var i = 0;  i < shopItems.length;  ++i )
    {
        appendShopItem( shopItems[i], subTotal );
    }

    var clearButton = document.createElement("BUTTON");
    var text = document.createTextNode("Clear Selection");
    clearButton.onclick = function () {
        subTotal = 0;
    };
    clearButton.appendChild(text);
    document.body.appendChild(clearButton);
}
函数appendShopItem(shopItem,小计)
{
//创建列表项:
var item=document.createElement('li');
//设置其内容:
item.appendChild(document.createTextNode(
shopItem.name+'-'+shopItem.cost+'Gold'
) );
//将其添加到列表中:
列表。追加子项(项目);
var radio=document.createElement('button');
var text=document.createTextNode(“购买”+shopItem.name+”表示“+shopItem.cost+”黄金”);
radio.name='shop';
radio.value=shopItem.name;
radio.onclick=函数(){
小计+=shopItem.cost;
附加值(购物项目,小计);
};
收音机.附件(文本);
文件.正文.附件(收音机);
var lineBreak=document.createElement(“BR”);
document.body.appendChild(换行符);
}
函数附加值(shopItem,小计)
{
document.getElementById(“extraSubHeader”).innerHTML=“当前总计:”+小计+“黄金”
}
功能输入商店(位置、位置名称、位置描述、按钮、按钮选项、商店物品、商店成本)
{
小计=0;
document.getElementById(“extraHeader”).style.visibility=“可见”;
document.getElementById(“extraHeader”).innerHTML=“欢迎光临商店!”;
document.getElementById(“extraSubHeader”).style.visibility=“visible”;
document.getElementById(“extraSubHeader”).innerHTML=“当前总计:0黄金”
document.getElementById(“列表”).style.visibility=“可见”;
document.getElementById(“option1”).style.visibility=“hidden”;
document.getElementById(“option2”).style.visibility=“hidden”;
document.getElementById(“optionInfo”).style.visibility=“hidden”;
document.getElementById(“子标题”).style.visibility=“隐藏”;
document.getElementById(“actionButton”).innerHTML=“离开商店”;
document.getElementById(“actionButton”).onclick=function(){loadData(位置、位置名称、位置描述、按钮、按钮操作、shopItems、shopCosts);hideShop();}
document.getElementById(“标题”).style.visibility=“隐藏”;
对于(变量i=0;i
变量
小计
根据所选项目的价格而增加。如果我多次单击该按钮,
小计
将增加。这工作正常,但也有一个按钮来重置总数。这就是问题所在;即使我将小计设置为0,它仍然会从上次停止的位置继续递增

例如,如果项目的价格为
10
,单击按钮
5
次将
小计
变成
50
。如果然后单击
清除选择
(“重置”按钮),
小计
应返回为
0
。但是,再次单击按钮将使
小计
变成
60
,而不是默认为
0
,然后变成
10


感谢您的帮助

由于闭包,似乎是一个典型的变量范围问题

appendShopItem
中,您拥有此事件处理程序

radio.onclick = function () {
    subTotal += shopItem.cost;
    addValue( shopItem, subTotal );
};
绑定到eventHandler的小计是调用
appendShopItem
时传递的内容

因此,即使您将
subTotal
重置为
0
,较早的值仍会绑定到它,并且只要单击单选按钮,就会使用
subTotal
的旧值


您可以进行重构,使
subTotal
不被传递,而是使其成为所有这些函数都可以访问的变量。

请发布最低相关代码,或者在小提琴中重新创建您的问题。这看起来像是演示文稿上的问题,而不是变量值错误。1。发布HTML2。你什么时候给商店打电话?3.你有没有考虑过使用角度传感器?jquery是创建一组表示数据的HTML元素的最糟糕的方法之一,因为它非常冗长,并且没有简单的方法来更新元素。这种方法非常有效。我只是在脚本的前面定义了
subTotal
,没有将其传递给函数,它现在可以工作了。