将所选单选按钮的值添加到方程式

Adding value of selected radio button to an equation

我束手无策。我不知道如何将单选按钮的值添加到等式中。有人至少可以给我一个线索吗?

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

<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)
}

除非您使用

,否则您的解决方案应该可以正常工作
console.log('value=' + selectedValue);

而不是实际返回值。

return selectedValue;

Example