简体   繁体   English

如何汇总单选按钮的各个列?

[英]How to sum individual columns of Radio buttons?

Via searching I can see how to sum the values of radio buttons in multiple columns into a single total. 通过搜索,我可以看到如何将多列中的单选按钮的值求和成一个总计。 How are several columns of radio buttons summed within their own column totals? 几列单选按钮如何在其自己的总列中求和?

I've used this to sum all columns into a single total: 我用它来将所有列求和成一个总数:

function setRadios()
 {

  var inputs = document.getElementById('myForm').getElementsByClassName('myRadio');
  function sumRadios(){
var total = 0, 
    oForm = this.form;

for(var x = 0; x < inputs.length; x++){
  if (inputs[x].checked) total += parseInt(inputs[x].value);
}

// Defines the field that totals the selections
oForm.elements['myxTN1'].value = total.toFixed(0);

  }

 for(var y = 0; y < inputs.length; y++){
inputs[y].onclick = sumRadios;
}

}
onload = setRadios;

If you have 2 or more columns of radio buttons, how do you sum for each column? 如果您有两列或更多列的单选按钮,您如何对每一列求和? Only one Entity is allowed to have each individual item so there is a fixed grand total. 每个实体只能有一个实体,因此有一个固定的总计。

In my JSFiddle http://jsfiddle.net/babakt/C4Swr/ selecting any button totals in one column. 在我的JSFiddle http://jsfiddle.net/babakt/C4Swr/中,选择任一列中的按钮总数。

How to fix so that Entity 1 selects item 1 for a total of 1; 如何修复,以使实体1选择项目1总计1; Entity 2 selects item 2 for a total of 2; 实体2选择项目2总计2; Entity 3 selects item 3 for a total of 10 and if Entity 1 selects item 3 the total for it changes to 11 and Entity 3 is 0? 实体3选择项目3的总数为10,如果实体1选择项目3,则其总数变为11,而实体3为0?

Thanks. 谢谢。

如果我正确理解了您的问题:只需为每列中的按钮和结果字段提供一个通用的类名(例如col1col2col3 ),然后使用getElementsByClassName('col1')等单独遍历这些列。

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

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