单选按钮值计算.第四种选择不会相加

Radio Button Value Calculation. The forth choice will not sum

本文关键字:选择 四种 计算 单选按钮      更新时间:2023-09-26

Fellas,

我有一个javscript,它计算单选按钮中定义的值。出于某种原因,我不明白为什么脚本跳过了一个组。似乎跳过的组(我所说的跳过是指所选的值不会被添加(与每个组中使用的单选按钮的数量相关。

我一直在为这件事揪头发。。

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript">
function setRadios()
{
function sumRadios(){
var total = 0, i = 0, oForm = this.form, e, len, j, c;
while(e=oForm.elements[i++]){
//This is probably the issue - Maybe it would be easier to manually define the names verses detecting them incrementally.
    if(e.name.match(/^m4j-'d/)){ 
        c=document.getElementsByName(e.name);
        for(j=0;j<c.length;j++){
            c[j].checked?total+=Number(c[j].value):null;
        i++;
        }
    }
}
// Defines the field that totals the selections
oForm.elements['m4j-65'].value = total.toFixed(0);
}
// Must define form name "m4jFrom"
var i = 0, input, inputs = document.getElementById('m4jForm').getElementsByTagName('input');
while (input = inputs.item(i++))
// This executes the sum onclick
if (input.name.match(/^m4j-'d/)) 
input.onclick = sumRadios;
}
onload = setRadios;
</script>
<style type="text/css"></style>
</head>
<body>
<form id="m4jForm" name="m4jForm" method="post" enctype="multipart/form-data" action="">
  <table  border="0" align="left" cellpadding="11" cellspacing="11" class="m4j_form_table">
    <tbody>
      <tr id="m4je-57"  >
        <td colspan="2" align="left" valign="top"><table border="1" style="width: 100%;" >
            <tbody>
              <tr>
                <td align="left" valign="top"><input class="m4jRadio" type="radio" id="m4j-57-0" name="m4j-57" value="1" >
                  </input>                  1 </td>
              </tr>
              <tr>
                <td align="left" valign="top"><input class="m4jRadio" type="radio" id="m4j-57-1" name="m4j-57" value="2" >
                  </input>
                  2 </td>
              </tr>
            </tbody>
          </table></td>
      </tr>
      <tr id="m4je-61"  >
        <td colspan="2" align="left" valign="top"><table border="1" style="width: 100%;" >
            <tbody>
              <tr>
                <td align="left" valign="top"><input class="m4jRadio" type="radio" id="m4j-61-0" name="m4j-62" value="1" >
                  </input>
                  1 </td>
              </tr>
              <tr>
                <td align="left" valign="top"><input class="m4jRadio" type="radio" id="m4j-61-1" name="m4j-62" value="2" >
                  </input>
                  2  </td>
              </tr>
            </tbody>
          </table></td>
      </tr>
      <tr id="m4je-62"  >
        <td colspan="2" align="left" valign="top"><table border="3" style="width: 100%;" >
            <tbody>
              <tr>
                <td align="left" valign="top"><input class="m4jRadio" type="radio" id="m4j-62-0" name="m4j-63" value="1" >
                  </input>
                  1 No Calculation </td>
              </tr>
              <tr>
                <td align="left" valign="top"><input class="m4jRadio" type="radio" id="m4j-62-1" name="m4j-63" value="2" >
                  </input>
                  2 if you remove this table the non calculation will move to the radios below</td>
              </tr>
            </tbody>
          </table></td>
      </tr>
      <tr id="m4je-63"  >
        <td colspan="2" align="left" valign="top"><table border="1" style="width: 100%;" >
            <tbody>
              <tr>
                <td align="left" valign="top"><div class="m4jSelectItem m4jSelectItemVertical">
                    <input class="m4jRadio" type="radio" id="m4j-63-0" name="m4j-64" value="1" >
                    </input>
                    1  </div></td>
              </tr>
              <tr>
                <td align="left" valign="top"><div class="m4jSelectItem m4jSelectItemVertical">
                    <input class="m4jRadio" type="radio" id="m4j-63-1" name="m4j-64" value="2" >
                    </input>
                    2  </div></td>
              </tr>
            </tbody>
          </table>
          </div>
          </div></td>
      </tr>
      <tr id="m4je-65"  >
        <td width="300" align="left" valign="top" >Total</td>
        <td width="0px;" align="left" valign="top" >
        <input class="m4jInputField" style="width: 100%;" id="m4j-65" name="m4j-65" type="text" size="18" maxlength="60" value= "" alt="" ></input><br>
          <input type="submit" name="submit" value="send" class ="m4j_submit" >
  </input>
  <input id="m4jResetButton" class ="m4j_reset" type="reset" name="reset" value="reset">
  </input>
          </td>
      </tr>
    </tbody>
  </table>
</form>
</body>
</html>

我找到了解决方案。我欠董事会信息。我希望有人能利用它

<script type="text/javascript">
function setRadios()
{
  var inputs = document.getElementById('m4jForm').getElementsByClassName('m4jRadio');
  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['m4j-65'].value = total.toFixed(0);
  }
  for(var y = 0; y < inputs.length; y++){
    inputs[y].onclick = sumRadios;
  }
}
onload = setRadios;
</script>