一直试图从ID标记totalDiv-totalDiv1中获取值,每个值显示所选值的结果,然后将它们加在一起以实时显示ID标记totalall中组合的值,但我一直得到NaN。
<h2>£10 Each</h2>
<select name="iteam1" onChange="calc1()">
<option value="10">1</option>
<option value="20">2</option>
<option value="30">3</option>
<option value="40">4</option>
<option value="50">5</option>
</select>
<div id="totalDiv1">£10</div>
<h2>£10 Each</h2>
<select name="iteam" onChange="calc()">
<option value="10">1</option>
<option value="20">2</option>
<option value="30">3</option>
<option value="40">4</option>
<option value="50">5</option>
</select>
<div id="totalDiv">£10</div>
<br>
<h2>Total</h2>
<div id="totalall">£0</div>
<script>
function calc() {
var fruitOrVeg = document.getElementsByName("iteam")[0].value;
var total = fruitOrVeg;
document.getElementById("totalDiv").innerHTML= "£" +(total);
}
</script>
<script>
function calc1() {
var fruitOrVeg = document.getElementsByName("iteam1")[0].value;
var total = fruitOrVeg;
document.getElementById("totalDiv1").innerHTML= "£" +(total);
}
</script>
<script>
window.onload = function total() {
var y = document.getElementById("totalDiv").value;
var z = document.getElementById("totalDiv1").value;
var x = +y + +z;
document.getElementById("totalall").innerHTML = "£" + x;
}
</script>
最佳答案
点要保重。
1.不要混淆价值和符号
2.您在div的Calc函数中添加了符号,后来又尝试将它们组合在一起,因此显然不是数字;
http://jsfiddle.net/ga16gt74/
<h2>£10 Each</h2>
<select name="iteam1" onChange="calc1()">
<option value="10">1</option>
<option value="20">2</option>
<option value="30">3</option>
<option value="40">4</option>
<option value="50">5</option>
</select>
<div id="totalDiv1">£
<span id="totalDiv1Value">0</span>
</div>
<h2>£10 Each</h2>
<select name="iteam" onChange="calc()">
<option value="10">1</option>
<option value="20">2</option>
<option value="30">3</option>
<option value="40">4</option>
<option value="50">5</option>
</select>
<div id="totalDiv">£
<span id="totalDivValue">0</span>
</div>
<br>
<h2>Total</h2>
<div id="totalall">£0</div>
<script>
function calc() {
var fruitOrVeg = document.getElementsByName("iteam")[0].value;
var value = fruitOrVeg;
document.getElementById("totalDivValue").innerHTML= value;
total();
}
function calc1() {
var fruitOrVeg = document.getElementsByName("iteam1")[0].value;
var value = fruitOrVeg;
document.getElementById("totalDiv1Value").innerHTML= value;
total();
}
function total() {
var y = parseInt(document.getElementById("totalDivValue").innerHTML);
var z = parseInt(document.getElementById("totalDiv1Value").innerHTML);
var x = y+z;
document.getElementById("totalall").innerHTML = "£" + x;
}
</script>