首页 > 解决方案 > 如何取消选中多个选择器并使用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";
    }

但是我没有做任何其他事情,我不知道如何进行减法并取消选中选择器。

标签: javascripthtmljquery

解决方案


如果您将函数 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";
}

请参阅此工作示例: https ://codepen.io/PhoenixFlight/pen/eYJNMYm


推荐阅读