简体   繁体   English

JavaScript 函数不起作用,但我确信一切都很好

[英]JavaScript funcion isn't working, but I'm sure everything's fine

The last function won't work, I don't know why.最后一个功能不起作用,我不知道为什么。 Tried changing the removeAttribute to setAttribute still won't work.尝试将 removeAttribute 更改为 setAttribute 仍然不起作用。 Any suggestions?有什么建议么?

        
        let mainEl = element.parentElement;
        let price = parseInt(mainEl.querySelector('p span').innerText);
        let name = mainEl.querySelector('h3').innerText;
        let vegetables = document.querySelectorAll('.single-item')
    
        AllTotal -= price;
        
        document.querySelector('.total').innerText=`Total: $${AllTotal}`;
    
        mainEl.remove();
    
        vegetables.forEach(function(vege){
            if (vege.querySelector('.si-content h3').innerText === name){
                
                vege.querySelector('.actions button').removeAttribute("disabled");
                vege.querySelector('.actions button').innerText = "Dodaj";
                vege.querySelector('.actions input').value = 0;
            };
        });
    };```

It does not work because in your compression if (vege.querySelector('.si-content h3').innerText === name){ it compares for example Krompir and Krompir: and since this has an extra : in the list , it cant find it.它不起作用,因为在您的压缩中if (vege.querySelector('.si-content h3').innerText === name){它比较了例如KrompirKrompir:并且由于它在列表中有一个额外的: ,它找不到它。

I have changed your code and removed : when you add items to your list from this <h3>${name}:</h3> to this <h3>${name}</h3>我已经更改了您的代码并删除了:当您将项目从这个<h3>${name}:</h3>添加到这个<h3>${name}</h3>

 let AllTotal = 0; function addToCart (elem){ let mainEl = elem.closest('.single-item'); let quantity = mainEl.querySelector('input').value; let name = mainEl.querySelector('.single-item h3').innerText; let price = mainEl.querySelector('.price').innerText; price = parseInt(price.substring(1)); if(quantity == 0){ alert('Dodajte kolicinu!'); return; } let total = price * quantity; document.querySelector('.cart-items').innerHTML += `<div class"cart-single-item"> <h3>${name}</h3> <p>${quantity} x $${price} = $<span>${total}</span></p> <button onclick="remove(this)" class="remove-item">Ukloni</button> </div>` elem.setAttribute('disabled', 'true'); elem.innerText ='Dodato' AllTotal += total; document.querySelector('.total').innerText=`Total: $${AllTotal}`; } function remove(element){ let mainEl = element.parentElement; let price = parseInt(mainEl.querySelector('p span').innerText); let name = mainEl.querySelector('h3').innerText; let vegetables = document.querySelectorAll('.single-item') AllTotal -= price; document.querySelector('.total').innerText=`Total: $${AllTotal}`; mainEl.remove(); vegetables.forEach(function(vege){ if (vege.querySelector('.si-content h3').innerText === name){ vege.querySelector('.actions button').removeAttribute('disabled'); vege.querySelector('.actions button').innerText = "Dodaj"; vege.querySelector('.actions input').value = 0; }; }); }
 <div class="container"> <div class="items"> <div class="single-item" data-type="krompir"> <div class="si-content"> <h3>Krompir</h3> <p class="price">$10</p> </div> <div class="actions"> <input type="number" name="quantity" value="0" min="0"> <button onclick="addToCart(this)">Dodaj</button> </div> </div> <div class="single-item" data-type="paradajz"> <div class="si-content"> <h3>Paradajz</h3> <p class="price">$20</p> </div> <div class="actions"> <input type="number" name="quantity" value="0" min="0"> <button onclick="addToCart(this)">Dodaj</button> </div> </div> </div> <div class="cart"> <h2>Korpa</h2> <div class="cart-items"></div> <div class="total"></div> </div> </div>

that being said this not ideal.话虽如此,这并不理想。 what you should be doing is separating UI and code logic by adding a data attribute or class name to recognize the list item instead of text inside html element.您应该做的是通过添加数据属性或类名来识别列表项而不是 html 元素内的文本来分离 UI 和代码逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 制作BASIC angularjs应用对我不起作用,我不确定为什么 - Making a BASIC angularjs app isn't working for me and I'm not sure why Onchange不起作用,但是javascript脚本很好 - Onchange isn't working but the javascript script is fine 基本的Javascript HTML表单-OnClick无法正常工作(我确定这很明显吗?) - Basic Javascript HTML form - OnClick not working (I'm sure it's something obvious?) 外部 Javascript 表单验证不起作用,不知道为什么? - External Javascript form validation isn't working, not sure why? JavaScript ID选择器不起作用? 不知道出了什么问题 - JavaScript id selector isn't working? not sure whats going wrong 不确定为什么这不起作用-Javascript基本增量功能 - Not sure why this isn't working - Javascript basic increment function 我不确定为什么这不是XML信息不会打印到屏幕上 - I'm not sure why this isn't XML information isn't printing to the screen 我正在尝试使用Google Maps Javascript API添加标记,但它无法正常工作 - I'm trying to add a marker using Google Maps Javascript API but it isn't working 我正在处理的故障效果不起作用 - The Glitch Effect I'm working on isn't working 我不知道为什么我的JavaScript无法正常工作…我正在将值动态传递给标签 - I can't figure out why my javascript isn't working… I'm dynamically passing values to a tag
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM