【发布时间】:2020-05-25 06:53:02
【问题描述】:
所以我想获取所有选定或选中的输入(复选框、单选按钮、选择选项)的值并将它们加在一起。但我也希望将这些值插入到数组中并更改数组中的值,如果它被取消选择或取消选中。
当任何输入被触发时,我想要对已更改、选择或检查的输入的所有值求和。
var gh=[];
$('#CheckBoxList').on('change', 'input[type=checkbox]', function() {
var id = $(this).val(); // this gives me null
var index = gh.indexOf(id);
if($(this).is(':checked')){
gh.push(id);
document.getElementById("demo").innerHTML='['+gh+']';
}
else{
if (index > -1) {
gh.splice(index, 1);
document.getElementById("demo").innerHTML='['+gh+']';
}
}
console.log(gh);
var sum = 0;
var gn, elem;
var gn, elem;
for (i=0; i<5; i++) {
gn = 'game'+i;
elem = document.getElementById(gn);
if (elem.checked == true) { sum += Number(elem.value); }
}
document.getElementById('totalcost').value = sum.toFixed(2);
});
$('#RadioButtonList').on('change', 'input[type=radio]', function() {
var id = $(this).val(); // this gives me null
var index = gh.indexOf(id);
if($(this).is(':checked')){
gh.push(id);
document.getElementById("demo").innerHTML='['+gh+']';
}
else{
if (index > -1) {
gh.splice(index, 1);
document.getElementById("demo").innerHTML='['+gh+']';
}
}
console.log(gh);
});
$('#quantity').change(function() {
var id = $(this).val(); // this gives me null
var index = gh.indexOf(id);
if($(this).is(':checked')){
gh.push(id);
document.getElementById("demo").innerHTML='['+gh+']';
}
else{
if (index > -1) {
gh.splice(index, 1);
document.getElementById("demo").innerHTML='['+gh+']';
}
}
console.log(gh);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- partial:index.partial.html -->
<div id="CheckBoxList">
<label><input type="checkbox" id='game0' value="1" name="Apple">Apple</label>
<label><input type="checkbox" id='game1' value="2" name="Orange">Orange</label>
<label><input type="checkbox" id='game2' value="3" name="Pineaple">Pineaple</label>
<label><input type="checkbox" id='game3' value="4" name="Lemon">Lemon</label>
<label><input type="checkbox" id='game4' value="5" name="Mango">Mango</label>
</div>
<div id="RadioButtonList">
<label><input type="radio" id='pad' value="6" name="HI">Small Cup</label>
<label><input type="radio" id='pad' value="7" name="HI">Medium Cup</label>
<label><input type="radio" id='pad' value="8" name="HI">Big Cup</label>
</div>
<div id="SelectOptions">
<select name="quantity" id="quantity">
<option value="">Select quantity</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</div>
<code id="demo"></code>
total cost<input type="text" id="totalcost" value="">
<br>total Checked<input type="text" id="checkedcount" value="">
<br>total boxes<input type="text" id="totalcount" value="">
到目前为止,我已经能够在复选框中取得成功。但是我发现很难包含单选按钮并选择选项输入以继续求和并附加复选框数组。我需要你的帮助。谢谢
【问题讨论】:
-
您好!您能否解释一下您的预期公式...只是数学上的。像“(选中复选框的总和 + 单选按钮的总和)* 数量)。关于数组的东西不清楚 :) 你想要它以序列化格式(选中元素的映射数组/Json 数组/本地存储数组)吗?还是它只是你在这种情况下提到的一个求和变量?
-
好的。我很想选择一个包含所有值的数组。这样,如果任何单选按钮或选定选项被更改,或者复选框被取消选中,它们的值将从数组中删除,总和也会改变。
标签: javascript jquery arrays checkbox radio-button