Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
试图更改“的值”;总价;变量,但我得到;“未定义”;在Javascript中_Javascript_Math_Increment_Decrement - Fatal编程技术网

试图更改“的值”;总价;变量,但我得到;“未定义”;在Javascript中

试图更改“的值”;总价;变量,但我得到;“未定义”;在Javascript中,javascript,math,increment,decrement,Javascript,Math,Increment,Decrement,我的目标是建立一个购物车,它能够计算n物品的总价格 我可以增加/减少每件物品的数量,因此一切正常。除了myUpdateCartTotal函数之外,它显示的是未定义的,而不是总价。我怎么能修好它 function getinput(event) { return event.target.parentElement.querySelector(".quantity"); } // the Event Listener document.addEventLi

我的目标是建立一个购物车,它能够计算
n
物品的总价格

我可以增加/减少每件物品的数量,因此一切正常。除了my
UpdateCartTotal
函数之外,它显示的是
未定义的
,而不是总价。我怎么能修好它

    function getinput(event) {
      return event.target.parentElement.querySelector(".quantity");
    }

 // the Event Listener 
    document.addEventListener("click", function(event) {
      if (event.target.className == "plus-btn") {
        increment(event);
        updateCarteTotal(event)
      }
      if (event.target.className == "minus-btn") {
        decrement(event)
        updateCarteTotal(event)
      }
    });

 // Increment function

    function increment(event) {
      var quantity = getinput(event)
      if(quantity.value<20){
         quantity.value++
        }
    }
  // Decrement function 
    function decrement(event) {
      var quantity = getinput(event)
      if(quantity.value >=1){
         quantity.value--
        }
    }

// the function to calculate the totale Carte price

    function updateCarteTotal(event) {

        const items=document.querySelectorAll(".item");
        var total_price=document.querySelector(".total_price");
        var quantity=getinput(event);
        var unit_price=document.querySelectorAll(".price");
        var total=0;
        for(item of items ){   
            total += parseInt(quantity.value * unit_price.value)
        }
        total_price.value=total.value
    }
函数getinput(事件){ 返回事件.target.parentElement.querySelector(“.quantity”); } //事件侦听器 document.addEventListener(“单击”),函数(事件){ 如果(event.target.className==“加上btn”){ 增量(事件); updateCarteTotal(事件) } 如果(event.target.className==“减去btn”){ 减量(事件) updateCarteTotal(事件) } }); //增量函数 函数增量(事件){ 变量数量=getinput(事件) 如果(数量值=1){ 数量价值-- } } //计算总点菜价格的函数 函数updateCarteTotal(事件){ const items=document.queryselectoral(“.item”); var总价=document.querySelector(“总价”); 变量数量=getinput(事件); var单价=document.queryselectoral(“.price”); var合计=0; (项目中的项目){ 总计+=parseInt(数量值*单价值) } 总价=总价 }
我看到的主要问题是,您试图从基元类型访问不存在的属性。基本体类型(如)与非基本体对象一样,没有可以访问的属性,因此:

total+=parseInt(quantity.value*unit\u price.value)
不起作用,因为
quantity
没有名为
value
的属性。对于
单价
变量也可以这样说。出于同样的原因,以下行将不起作用:
total\u price.value=total.value

另外,
total\u price
是本地范围内的函数
updateCarteTotal
,因此在程序期间不会保留值。您最好在任何单个函数的作用域之外创建一个全局变量来存储您的购物车总值