在函数

时间:2017-12-30 22:44:50

标签: javascript

一般来说,我是一个完整的编码新手 - 经过一些阅读和一些YouTube视频后尝试这个!

我有两个单独的Javascript函数,它们使用基本的加法来返回我的HTML中的id的数字。这些工作正常,但我想使用第三段Javascript将'personal_items'和'cash_total'数字加在一起并显示它们。

我已将大部分变量移到函数之外,使它们全局化,因此可以在任何地方使用它们 - 但HTML不会返回calcTotalAssets()函数的值。

正如我所说,我是全新的,所以任何帮助都会受到高度赞赏?

<script>
        var current_value_of_home;
        var current_value_other_properties;
        var personal_items;
        var current_accounts;
        var savings_accounts;
        var isa;
        var life_insurance;
        var family_friends;
        var cash_other;
        var cash_total;
        var total_assets;

        function calcPersonalItems(){
            current_value_of_home = document.getElementById('current_value_of_home').value;
            current_value_other_properties = document.getElementById('current_value_other_properties').value;
            personal_items = Number(current_value_of_home) + Number(current_value_other_properties);
            personal_items = personal_items.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
            document.getElementById('personal_items').innerHTML = "Personal Items = £"+personal_items;
        }

        function calcCashTotal(){
            current_accounts = document.getElementById('current_accounts').value;
            savings_accounts = document.getElementById('savings_accounts').value;
            isa = document.getElementById('isa').value;
            life_insurance = document.getElementById('life_insurance').value;
            family_friends = document.getElementById('family_friends').value;
            cash_other = document.getElementById('cash_other').value;
            cash_total = Number(current_accounts) + Number(savings_accounts) + Number(isa) + Number(life_insurance) + Number(family_friends) + Number(cash_other);
            cash_total = cash_total.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
            document.getElementById('cash_total').innerHTML = "Cash or Cash Equivalent = £"+cash_total;
        }

        function calcTotalAssets(){
            total_assets = Number(personal_items) + Number(cash_total);
            total_assets = total_assets.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
            document.getElementById('total_assets').innerHTML = "Assets = £"+total_assets;
        }

    </script>

使用JavaScript的HTML - 底部的输出部分应显示最终的组合数字;

 <div class="col-sm-4" style="background-color:lavender;">
                <h2>Personal Items</h2>
                <p>Current Value of Home: £<input id="current_value_of_home" type="number" min="0" max="5000000" step="1000" onchange="calcPersonalItems()"></p>
                <p>Current Value of Other Properties: £<input id="current_value_other_properties" type="number" min="0" max="10000000" step="1000" onchange="calcPersonalItems()"></p>
                <h3 id="personal_items"></h3>
                <h2>Cash or Cash Equivalent</h2>
                <p>Current Account(s): £<input id="current_accounts" type="number" min="0" max="5000000" step="100" onchange="calcCashTotal()"></p>
                <p>Savings Account(s): £<input id="savings_accounts" type="number" min="0" max="5000000" step="100" onchange="calcCashTotal()"></p>
                <p>ISA: £<input id="isa" type="number" min="0" max="5000000" step="100" onchange="calcCashTotal()"></p>
                <p>Life Insurance (cash value): £<input id="life_insurance" type="number" min="0" max="5000000" step="100" onchange="calcCashTotal()"></p>
                <p>Money Owed to You from Family/Friends: £<input id="family_friends" type="number" min="0" max="5000000" step="100" onchange="calcCashTotal()"></p>
                <p>Other: £<input id="cash_other" type="number" min="0" max="5000000" step="100" onchange="calcCashTotal()"></p>
                <h3 id="cash_total"></h3>
            </div>
            <div class="col-sm-4" style="background-color:orange;">
            </div>
            <div class="col-sm-4" style="background-color:yellow;">
                <p>Output</p>
                <h2 id="total_assets"></h3>
            </div>

0 个答案:

没有答案