0

伙计们,

我有一个 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>
4

1 回答 1

0

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

<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>
于 2013-05-06T14:39:04.243 回答