2015-03-13 57 views
0

我在我的智慧结尾。我无法弄清楚如何将单选按钮的值添加到方程中。有人可以给我一个线索,至少在这个?将所选单选按钮的值添加到公式

如果我没有包含单选按钮功能,这工作正常。

<input type="radio" class="InternetCost" name="int" id="no_int" value="0">None<br> 
<input type="radio" class="InternetCost" name="int" id="ten" value="10">10<br> 
<input type="radio" class="InternetCost" name="int" id="twenty" value="20">20<br> 
<input type="radio" class="InternetCost" name="int" id="thirty" value="30">30<br> 

//Try to select radio button 
var values = document.getElementsByName("int"); 

function getValue() { 
    for(var i = 0; i < values.length; i++) { 
    if(values[i].checked == true) { 
    selectedValue = values[i].value; 
    console.log('value=' + selectedValue); 
    } 
    } 
} 


function calculate() 
{ 

//get selectedValue - this is where I am lost! 
var radioValue = getValue()*1; 

//Package 
var pkgCost = document.getElementById("pkg").value*1; 

//Package - Static 
var equipPkg = document.getElementById("pep").value*1; 

//Additional amount 
var addBox = document.getElementById("addtl").value*1; 

//Additional Box cost - static 
var totalNum = document.getElementById("add_cost").value*1; 

//Service amount 
var dvrSvc = document.getElementById("dvr_svc").value*1; 

//Service cost - static 
var dvrCost = document.getElementById("dvr_cost").value*1; 

// Sum everything 
var SumAll = pkgCost + equipPkg + (addBox*totalNum) + (Svc*Cost) + radioValue; 

// print the total 
document.getElementById("Sum").innerHTML = SumAll.toFixed(2) 
} 

回答

0

您的解决方案应该,除非你正在使用

console.log('value=' + selectedValue); 

,而不是实际返回值正常工作。

return selectedValue; 

Example

+0

这是正确的答案。 – 2015-03-13 18:46:08

相关问题