如何将表单数据的输入结果加在一起

时间:2015-05-05 02:28:35

标签: javascript html

如何在下面创建一个会发布所有添加在一起的值的部分?

<p>Your personal financial advisor.</p>
<br />
<p>How much do you may every two weeks?</p>
    <table border= "1px">
        <tr>
            <td><form>
                <input id="quantity" class="qty" type="text" placeholder="quantity" />
<br>
                <input type="button" value="Calculate" id="calc"/>
                </form>
                    <script>

                    document.getElementById('calc').addEventListener('click', updater);

                    function updater () {
                        var cost = 2;
                             var quantity = document.getElementById('quantity').value;
                                var totalcost = (cost * quantity);

                    document.getElementById('total').innerText = totalcost;
                    }
                    </script>

            </td>
        </tr>
    </table>
<p>How much is your rent or mortgage per month?</p>
    <table border= "1px">
        <tr>
            <td><form>
                <input id="mortgage" class="mrg" type="text" placeholder="mortgage" />
<br>
                <input type="button" value="Calculate" id="calc2"/>
                </form>
                    <script>

                    document.getElementById('calc2').addEventListener('click', updater);

                    function updater () {
                         var payments = 1;
                            var mortgage = document.getElementById('mortgage').value;
                                var totalcost2 = (payments * mortgage);

                    document.getElementById('total2').innerText = totalcost2;
                    }
                    </script>



            </td>
         </tr>
    </table>

理想情况下,我希望这里有一个声明:“您的每月总费用是:计算完成后每个输入值的总和。我该怎么做?

如何重新调用输出值以创建计算?

1 个答案:

答案 0 :(得分:0)

只是为了告诉你如何获得输出,只需使用你的代码。 我在http://jsfiddle.net/davygxyz/3rrb5fqs/2/处有一个例子。

您的个人理财顾问。


每两周你可以多少钱?

<form>
    <input id="quantity" class="qty" type="text" placeholder="quantity" />
</form>

您每月的租金或抵押贷款金额是多少?

<form>
    <input id="mortgage" class="mrg" type="text" placeholder="mortgage" />
    </form>

    <button id="calc" >Calculate</button>
    <div id='total'></div>

<script>

    document.getElementById('calc').addEventListener('click', updater);

    function updater () {
    //calculate how much the person makes every two weeks.
    var quantity = document.getElementById('quantity').value;
    //since your asking for every two weeks, I set a variable below equal to 26 instead of 52.
    var weeks = 26 
    var q_total = quantity * weeks;

    //Rent or mortgage per month

    var months = 12;
    var mortgage = document.getElementById('mortgage').value;
    var yr_mortage = mortgage*months;


 // subtract expenses 

 //Create variable total, takes the total quantity and substract your expenses.
 var total = q_total - yr_mortage;


 document.getElementById('total').innerText = "Your Income is "+q_total+" dollars for the Year. \n Your expense is "+yr_mortage+' dollars for the year.\n All together your funds come to '+total+' dollars.';


//You can console most of your work to check it also. Example:

console.log(yr_mortage,q_total,total);

</script>