javascript - 如何取消选中多个选择器并使用js执行减法?
问题描述
我有三个下拉菜单。每次从下拉列表中选择一个项目时,它都会被添加到总值中,并显示在标签的下方。当用户点击标签的“X”时,标签被移除并且项目的值应该从下拉列表中减去。但是,它没有被减去。
这是我有表格的链接:
https://codepen.io/MoisosxD/pen/gOPpXGQ
您会看到像这样删除我拥有的标签:
onclick="desmarcarspan1();"
Dentro del js así:
function desmarcarspan1(){
var total = document.getElementById("totalGeneral");
var total1 = new Array(total);
var NuevoTotal = total.textContent;
console.log(total.textContent);
var ans10 = document.getElementById("ans-10");
ans10.style.display = "none";
}
但是我没有做任何其他事情,我不知道如何进行减法并取消选中选择器。
解决方案
如果您将函数 sumValues() 设为全局,则只需取消选中下拉元素并调用该函数:
和值():
function sumValues() {
const inputArray = document.querySelectorAll('input.itemTotalNeto');
console.log(inputArray);
const totalValue = document.querySelector('#totalGeneral');
let cboxVals = 0;
inputArray.forEach(element => cboxVals += element.checked ? parseInt(element.value, 10) : 0);
totalValue.innerHTML = cboxVals;
}
demarcspan1():
function desmarcarspan1() {
//decheck the dropdown-element:
document.getElementById('check-1').checked = false;
//sum up values again
sumValues();
var total = document.getElementById("totalGeneral");
var total1 = new Array(total);
var NuevoTotal = total.textContent;
console.log(total.textContent);
const totalValue = document.querySelector('#totalGeneral');
var ans1 = document.getElementById("ans-1");
ans1.style.display = "none";
}
推荐阅读
- node.js - 如何使用 NodeJS 在控制台中获取用户鼠标位置?
- javascript - 我想在组件中使用 vue-d3-charts 呈现条形图,但条形图未呈现
- wordpress - 永久链接 - Polylang 翻译 - Wordpress 单页
- windows - WinDBG 内核断点用户态进程
- android - 以编程方式将glb文件存储在firebase存储中
- batch-file - 即使以管理员身份运行,访问也被拒绝
- php - 使用 curl Php 发送 csv 文件
- ios - 如何加载某些低优先级的请求
- python - 更改以给定字符串开头的所有类属性的最佳方法是什么?
- c++ - 在调用其中声明的方法时是否应该包含文件?