[英]I'm trying to assign a variable to check-boxes and adding that variable to an output variable when it's checked
I'm very new to Javascript and would appreciate ANY help! 我是Java的新手,将不胜感激! I'm also using a jQuery library if that changes anything.
如果这有任何改变,我也使用jQuery库。
What I need is that if the first checkbox
was ticked the output should be 100kcal, while if both were ticked then it should add up to 300kcal. 我需要的是,如果第一个
checkbox
被选中,则输出应为100kcal,而如果两个都被选中,则其总和应为300kcal。 My problem is that when I untick it adds the variables AGAIN. 我的问题是,当我取消选中它时,会再次添加变量。
HTML: HTML:
<input type=checkbox onchange="myFunction(100)" value="scrambledEggs">Scrambled Eggs</input>
<input type=checkbox onchange="myFunction(200)" value="bacon">Bacon</input>
<p id="output">0kcal</p>
JS: JS:
var result = 0;
function myFunction(x) {
if (this.checked) {
result -= x;
document.getElementById("output").innerHTML = result + "kcal";
}
else {
result += x;
document.getElementById("output").innerHTML = result + "kcal";
}
}
Firstly if you're using jQuery, you should use it to attach the event handlers instead of onchange
attributes. 首先,如果您使用的是jQuery,则应使用它来附加事件处理程序而不是
onchange
属性。 Secondly, the input
tag is self closing - your current HTML is invalid. 其次,
input
标记是自动关闭的-您当前的HTML无效。 Finally, you can use a data
attribute to store the kcal value for the option: 最后,您可以使用
data
属性存储该选项的kcal值:
<label><input type="checkbox" class="food-option" data-kcals="100" value="scrambledEggs" />Scrambled Eggs</label>
<label><input type="checkbox" class="food-option" data-kcals="200" value="bacon" />Bacon</label>
<p id="output"><span>0</span>kcal</p>
Then you can use jQuery to attach the event and total up all the checked values and display them: 然后,您可以使用jQuery附加事件并汇总所有检查的值并显示它们:
$('.food-option').change(function() {
var totalKcals = 0;
$('.food-option:checked').each(function() {
totalKcals += parseInt($(this).data('kcals'), 10);
});
$('#output span').text(totalKcals);
});
In your case you can use this code: 您可以使用以下代码:
HTML HTML
<input type="checkbox" value="scrambledEggs" data-kcal="100">scrambledEggs</input>
<input type="checkbox" value="bacon" data-kcal="200">bacon</input>
<p id="output"> 0 kcal</p>
it have data-kcal
tag which is container for your kcal value. 它具有
data-kcal
标签,该标签是您的kcal值的容器。
JS JS
var result = 0;
$('input[type="checkbox"]').on("change", function() {
if($(this).attr('checked'))
{
result += parseInt($(this).attr("data-kcal"));
}else{
result -= ($(this).attr("data-kcal"));
}
$("#output").text(result + " kcal");
});
Also you can check how it works on this jsFiddle . 您也可以检查它如何在此jsFiddle上工作 。
Your HTML should be like below 您的HTML应该如下所示
<input type='checkbox' value="100">Scrambled Eggs </input>
<input type='checkbox' value="200"> Bacon </input>
<p id="output">0kcal </p>
Then you better use JQuery, less code written, more readability. 然后,您最好使用JQuery,减少编写的代码,提高可读性。 The code below will achieve your needs.
下面的代码将满足您的需求。
$('input[type=checkbox]').change(function (e) { //This will trigger every check/uncheck event for any input of type CheckBox.
var res = 0;
$('input[type=checkbox]:checked').each(function() { //Loop through every checked checkbox.
res += parseInt($(this).val()); //Sum it's value.
});
$('#output').text(res); //Add the final result to your span.
});
Pass the element that is clicked into the function... 将点击的元素传递到函数中...
HTML HTML
<input type=checkbox onchange="myFunction(this, 200)" value="bacon">Bacon</input>
JAVASCRIPT JAVASCRIPT
function myFunction(element, value) {
console.log(element.checked);
}
Check this JSFiddle for a demo. 在此JSFiddle中查看演示。
Better way of doing it is like this... 更好的方法是这样的...
HTML HTML
<div id="checkboxes">
<input type=checkbox value="bacon">Bacon</input>
<input type=checkbox value="Other">Other</input>
</div>
<p id="output">0kcal</p>
JAVASCRIPT JAVASCRIPT
var checkboxes = document.getElementById("checkboxes");
checkboxes.onchange = function (e) {
alert("Target: " + e.target.value + " Checked: " + e.target.checked);
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.