计算DIV内容的总和

发布于 2024-08-25 06:32:49 字数 2098 浏览 8 评论 0原文

我有一个带有几个选择框的表单,并根据用户的选择显示一个值。这是完全有效的。

现在我想要一个脚本,可以自动或在按下按钮时计算这些值的总和。

到目前为止,我拥有的完整代码是这样的:

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 = "&euro; " +this.value;
    }

    value2=document.getElementById('value2'), 
    combobox2=document.getElementById('second');

    combobox2.onchange=function(b)
    {
    document.getElementById('value2').innerHTML = "&euro; " +this.value;
    }
}
function changeText(){
var sum;    
sum= (value * 1) + (value2 * 1);
document.getElementById('sum').innerHTML = "&euro; " +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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情愿 2024-09-01 06:32:49

我稍微修改了您的 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;    
div1Value = document.getElementById('value1').innerHTML.replace(/[^0-9]/g, '')*1;
div2Value = document.getElementById('value2').innerHTML.replace(/[^0-9]/g, '')*1;
sum = div1Value + div2Value;
document.getElementById('sum').innerHTML = "€ " +sum;
}​

示例如下: http:// jsbin.com/awici/2

但是,我建议您使用其他方法,因为这个方法有点不稳定,并将数字作为字符串处理。

一个好的方法是将值存储在隐藏字段中,然后简单地计算其中的值。

希望这能澄清问题并对您有所帮助。有什么不懂的地方可以随时提问

I've modified your JavaScript slightly to cater for the calculation, and now it works fine:

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;    
div1Value = document.getElementById('value1').innerHTML.replace(/[^0-9]/g, '')*1;
div2Value = document.getElementById('value2').innerHTML.replace(/[^0-9]/g, '')*1;
sum = div1Value + div2Value;
document.getElementById('sum').innerHTML = "€ " +sum;
}​

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文