2013-05-03 84 views
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> 
+0

jsFiddle for try the code? – 2013-05-03 19:12:11

回答

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>