简体   繁体   English

从复选框标签添加值

[英]Add values from checkbox labels

I have 4 checkboxes with labels and a total label in my webpage. 我的网页上有4个带有标签和总标签的复选框。 The labels are the price eg $12, $100. 标签是价格,例如$ 12,$ 100。 When the user checks a box I want to take the value from the label and put it in the total label. 当用户选中一个框时,我想从标签中获取值并将其放入总标签中。 Then if the user deselect the box, then subtract that value from the total label. 然后,如果用户取消选择该框,则从总标签中减去该值。

I have tried to set a function called checkbox2() where I took the value and stripped the '$' then turned the remaining string into a number. 我试图设置一个名为checkbox2()的函数,在该函数中我取了值并剥离了“ $”,然后将剩余的字符串转换为数字。 Then checked if the box was checked, if so, then add number. 然后检查是否选中了该框,如果已选中,则添加数字。 then convert back to string and set the innerHTML. 然后转换回字符串并设置innerHTML。 Did not work and I am sure not the way to do this. 没有用,我确定不是这样做的方法。

How should I go about this? 我应该怎么做?

<div class="cbwrap">
    <label for="check2" name="label2" id="label2">
        <input type="checkbox" class="cb" id="check2" onclick="checkBox2()"/> $125
    </label>
</div>
<div class="cbwrap">
    <label>
        <input type="checkbox" class="cb" id="check3" onclick="checkBox2()"/> $100
    </label>
</div>
<div class="cbwrap">
    <label>
        <input type="checkbox" class="cb" onclick="checkBox2()" /> $75 
    </label>
</div>
<div class ="cbwrap">
    <label>
        <input type="checkbox" class="cb" onclick="checkBox2()" /> $50 
    </label>
</div>
<div class ="pwrap">
    <p class="cat1"><b><u>Total</u></b></p>
</div>
<div class="cbwrap">
    <label for="total" name="totallbl" id="totallbl"><u><b>$0</b></u></label>
</div>
<div> 
    <label for="total" name="agreedLBL" id="agreedLBL">0</label>
</div>

JS: JS:

var k = document.getElementById("totallbl").innerHTML;
if (document.getElementById("check1").checked) {
    x = "150";
} else {
    x = "0";
    var res = k + x;
    document.getElementById("totallbl").innerHTML = res;
}

your html was also not correct. 您的html也不正确。 Try this 尝试这个

 function checkBox2(checkbox) { if (checkbox.checked) { var value = checkbox.parentNode.textContent.match(/\\s*\\d+(?:\\.\\d{2})?/)[0]; var totalValue = document.getElementById('totallbl').querySelector('b').innerHTML.match(/\\s*\\d+(?:\\.\\d{2})?/)[0]; var newTotalValue = parseFloat(value) + parseFloat(totalValue); document.getElementById('totallbl').querySelector('b').innerHTML = "$" + newTotalValue; document.getElementById('agreedLBL').innerText = newTotalValue; } else { var value = checkbox.parentNode.textContent.match(/\\s*\\d+(?:\\.\\d{2})?/)[0]; var totalValue = document.getElementById('totallbl').querySelector('b').innerHTML.match(/\\s*\\d+(?:\\.\\d{2})?/)[0]; var newTotalValue = parseFloat(totalValue) - parseFloat(value); document.getElementById('totallbl').querySelector('b').innerHTML = "$" + newTotalValue; document.getElementById('agreedLBL').innerText = newTotalValue; } } 
 <div class="cbwrap"> <label for="check2" name="label2" id="label2"> <input type="checkbox" class="cb" id="check2" onclick="checkBox2(this)" /> $125</label> </div> <div class="cbwrap"> <label> <input type="checkbox" class="cb" id="check3" onclick="checkBox2(this)" /> $100</label> </div> <div class="cbwrap"> <label> <input type="checkbox" class="cb" onclick="checkBox2(this)" /> $75</label> </div> <div class="cbwrap"> <label> <input type="checkbox" class="cb" onclick="checkBox2(this)" /> $50</label> </div> <div class="pwrap"> <p class="cat1"><b><u>Total</u></b></p> </div> <div class="cbwrap"> <label for="total" name="totallbl" id="totallbl"><u><b>$0</b></u></label> </div> <div> <label for="total" name="agreedLBL" id="agreedLBL">0</label> </div> 

Basically, you should be setting html in a way so that it's easy to access values associated with checkboxes. 基本上,您应该以某种方式设置html,以便轻松访问与复选框关联的值。 Please note data-value="0" on totallbl and value assigned for each input checkbox. 请注意totallbl上的data-value =“ 0”以及为每个输入复选框分配的

 function checkBox2(obj) { var k = parseInt(document.getElementById("totallbl").dataset.value); if (obj.checked) { k += parseInt(obj.value); } else { k -= parseInt(obj.value); } document.getElementById("agreedLBL").innerHTML = k; document.getElementById("totallbl").dataset.value = k; document.getElementById("totallbl").innerHTML = '$' + k; } 
 <div class="cbwrap"><label for="check1" name="label2" id="label2"><input type="checkbox" class="cb" id="check1" onclick="checkBox2(this);" value="150" /> $150</label></div> <div class="cbwrap"><label for="check2" name="label2" id="label2"><input type="checkbox" class="cb" id="check2" onclick="checkBox2(this);" value="125" /> $125</label></div> <div class="cbwrap"><label><input type="checkbox" class="cb" id="check3" onclick="checkBox2(this);" value="100" /> $100</label></div> <div class="cbwrap"><label><input type="checkbox" class="cb" onclick="checkBox2(this);" value="75" /> $75</label></div> <div class="cbwrap"><label><input type="checkbox" class="cb" onclick="checkBox2(this);" value="50" /> $50</label></div> <div class="pwrap"><p class="cat1"><b><u>Total</u></b></p></div> <div class="cbwrap"><label for="total" name="totallbl" id="totallbl" data-value="0" style="font-weight:bold;text-decoration:underline">$0</label></div> <div> <label for="total" name="agreedLBL" id="agreedLBL">0</label></div> 

HEllo Dear it complete running example and please add jquery cdn in head tag. HEllo亲爱的它完整的运行示例,请在head标签中添加jquery cdn。 if you not getting exact output then tell me.. 如果您没有得到准确的输出,请告诉我。

    <body>
        <div class="cbwrap">
            <label for="check2" name="label2" id="label2">
                <input type="checkbox" class="cb" id="check2" onclick="checkBox2(this)" />
                $125</label>
        </div>
        <div class="cbwrap">
            <label>
                <input type="checkbox" class="cb" id="check3" onclick="checkBox2(this)" />
                $100</label>
        </div>
        <div class="cbwrap">
            <label>
                <input type="checkbox" class="cb" onclick="checkBox2(this)" />
                $75</label>
        </div>

        <div class="cbwrap">
            <label>
                <input type="checkbox" class="cb" onclick="checkBox2(this)" />
                $50</label>
        </div>

        <div class="pwrap">
            <p class="cat1">
                <b><u>Total</u></b>
            </p>
        </div>
        <div class="cbwrap">
            <label for="total" name="totallbl" id="totallbl"><u><b>$0</b></u></label>
        </div>
        <div>
            <label for="total" name="agreedLBL" id="agreedLBL">0</label>
        </div>
        <script>
            function checkBox2(tempCheckBOx) {
                var tempLenght = $(".cbwrap label input[type='checkbox']").length;
                var tempTotal = 0;
                for (var i = 0; i < tempLenght; i++) {
                    if ($(".cbwrap label input[type='checkbox']").eq(i).prop('checked') == true) {
                        var tempStore = $(".cbwrap label").eq(i).text();
                        var tempVal = parseInt(tempStore.trim().substr(1, (tempStore.length + 1)));
                        tempTotal += tempVal;
                    }
                }
               $("#agreedLBL").text("$"+tempTotal);
            }
        </script>
    </body> 

$(document).ready(function(){ $(文件)。就绪(函数(){

    $('.cb').click(function(){

        var totalvaluestored =  document.getElementById('totallbl').innerText.replace(/\$/g, '');
        var value = this.value;
        if(totalvaluestored === "0"){

            document.getElementById('totallbl').innerHTML = '$'+value;
            this.value = "";
        }
        else if(totalvaluestored !== "0" && value !== "")
        {

            value = parseInt(totalvaluestored) + parseInt(value);
            document.getElementById('totallbl').innerHTML = '$ '+value;
            this.value = "";
        }
        else{

            var value = this.closest('label').innerText;
            value = value.replace(/\$/g, '');
            this.value = value;
            value = parseInt(totalvaluestored) - parseInt(value);
            document.getElementById('totallbl').innerHTML = '$ '+value;

        }
    });
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM