将所选单选按钮的值添加到等式中

时间:2015-03-13 18:27:26

标签: javascript radio-button

我在我的智慧结束。我无法弄清楚如何将单选按钮的值添加到等式中。至少有人能给我一个线索吗?

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

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

1 个答案:

答案 0 :(得分:0)

除非您正在使用

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

而不是实际返回值。

return selectedValue;

Example