计算DIV内容的总和
我有一个带有几个选择框的表单,并根据用户的选择显示一个值。这是完全有效的。
现在我想要一个脚本,可以自动或在按下按钮时计算这些值的总和。
到目前为止,我拥有的完整代码是这样的:
JavaScript:
<script type="text/javascript">
window.onload=function(a,b)
{
value=document.getElementById('value1'),
combobox=document.getElementById('first');
combobox.onchange=function(a)
{
document.getElementById('value1').innerHTML = "€ " +this.value;
}
value2=document.getElementById('value2'),
combobox2=document.getElementById('second');
combobox2.onchange=function(b)
{
document.getElementById('value2').innerHTML = "€ " +this.value;
}
}
function changeText(){
var sum;
sum= (value * 1) + (value2 * 1);
document.getElementById('sum').innerHTML = "€ " +this.value;
}
</script>
HTML:
<ol>
<li><label><span>First</span></label>
<select name="block1" id="first">
<option value="0">Please select...</option>
<option value="1">Summer</option>
<option value="2">Spring</option>
</select></li>
</ol>
<ol>
<li><label><span>Second</span></label>
<select name="block2" id="second">
<option value="0">Please select...</option>
<option value="1">Summer</option>
<option value="2">Spring</option>
<option value="3">Pink</option>
<option value="4">Corporate</option>
</select></li>
</ol>
<div id="value1">value 1</div>
<div id="value2">value 2</div>
<input type='button' onclick='changeText()' value='Calculate'/>
<div id="sum">Total here</div>
如果有人可以提供帮助,那就太好了。我实在想不通。非常非常感谢!!
I have a form with several SELECT boxes and based on the user's selection a value appears. This is fully working.
Now I want a script that calculates the SUM of these values either automatically or when pressing a button.
The full code I have so far is this:
The JavaScript:
<script type="text/javascript">
window.onload=function(a,b)
{
value=document.getElementById('value1'),
combobox=document.getElementById('first');
combobox.onchange=function(a)
{
document.getElementById('value1').innerHTML = "€ " +this.value;
}
value2=document.getElementById('value2'),
combobox2=document.getElementById('second');
combobox2.onchange=function(b)
{
document.getElementById('value2').innerHTML = "€ " +this.value;
}
}
function changeText(){
var sum;
sum= (value * 1) + (value2 * 1);
document.getElementById('sum').innerHTML = "€ " +this.value;
}
</script>
The HTML:
<ol>
<li><label><span>First</span></label>
<select name="block1" id="first">
<option value="0">Please select...</option>
<option value="1">Summer</option>
<option value="2">Spring</option>
</select></li>
</ol>
<ol>
<li><label><span>Second</span></label>
<select name="block2" id="second">
<option value="0">Please select...</option>
<option value="1">Summer</option>
<option value="2">Spring</option>
<option value="3">Pink</option>
<option value="4">Corporate</option>
</select></li>
</ol>
<div id="value1">value 1</div>
<div id="value2">value 2</div>
<input type='button' onclick='changeText()' value='Calculate'/>
<div id="sum">Total here</div>
If anybody could help, that would be great. I really can't figure it out. Many many thanks!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我稍微修改了您的 JavaScript 以适应计算,现在它工作正常:
示例如下: http:// jsbin.com/awici/2
但是,我建议您使用其他方法,因为这个方法有点不稳定,并将数字作为字符串处理。
一个好的方法是将值存储在隐藏字段中,然后简单地计算其中的值。
希望这能澄清问题并对您有所帮助。有什么不懂的地方可以随时提问
I've modified your JavaScript slightly to cater for the calculation, and now it works fine:
Example here: http://jsbin.com/awici/2
However, I'd suggest you used some other approach, as this one is a bit flaky, and deal with numbers as strings.
A good approach, would be to store the values in hidden fields, and then simply calculate the values in there.
Hope this clear things out and helps you. Feel free to ask anything you don't understand