函数未记录全局范围变量

时间:2017-03-01 21:33:19

标签: javascript jquery

我正在尝试根据全局范围变量集计算抵押值,但该函数未记录PV值。我在这做错了什么?请不要对代码结构发表评论,我知道这很糟糕。

<script>
    jQuery( document ).ready(function() {

        /* Logo */
        jQuery("body > nav > div > div > div.navbar-header > h1 > a > img").attr('src', 'https://www.mediapreset.com/clients/spotzer/fenwick/wp-content/uploads/2017/03/FenwickMortgageLogoMMwide.jpg');

        /* Remove theme elements */
        jQuery("section#clients").remove();
        jQuery("section#projects").remove();
        jQuery("section#testimonials").remove();
        jQuery("section#services").remove();
        jQuery("section#calltoaction").remove();
        jQuery("section#about").remove();
        jQuery("section#skills").remove();
        jQuery("section#team").remove();
        jQuery("section#blog").remove();
        jQuery("section#newsletter").remove();

        /* Remove theme elements */
        jQuery('#features').attr('id', 'mortgage');

        /* In put fields for mortgage calculator */
        var borrowAmountLabel  = '<label>How much do you want to borrow?</label>';
        var borrowAmountTotal = '<input type="number"></input>';
        var borrowAmountValue = jQuery('#work > div > div > div.row > div:nth-child(1) > input[type="number"]').val();

        var totalYearsLabel = '<label>Over how many years?</label>';
        var totalYearsInput = '<input type="number"></input>';
        var totalYearsValue = jQuery('#work > div > div > div.row > div:nth-child(2) > input[type="number"]').val();

        var interestRateLabel = '<label>Interest rate</label>';
        var interestRateInput = '<input type="number"></input>';
        var interestRateValue = jQuery('#work > div > div > div.row > div:nth-child(3) > input[type="number"]').val();

        /* Reconstruct features section */
        jQuery('#work > div > div > div.col-md-12.heading > h2 > span').text('Mortgage Calculator');
        jQuery('#work > div > div > div.col-md-12.heading > p').remove();

        /* Make multicolumn, empty the div, and add columns */
        jQuery('#work > div > div').attr('class', 'row multi-columns-row');
        jQuery('#work > div > div > div:nth-child(2)').remove();

        jQuery('#work > div > div > div').after('<div class="row">' + '<div class="col-md-4">' + borrowAmountLabel + borrowAmountTotal + '</div>' + '<div class="col-md-4">' + totalYearsLabel + totalYearsInput + '</div>' + '<div class="col-md-4">' + interestRateLabel + interestRateInput + '</div>' + '</div>');

        jQuery('#work > div > div > div:nth-child(4)').after('<button type="submit">Submit<button>');

        jQuery('#work > div > div > div.row').after('<button type="submit">Calculate</button>');

        var calculateButton = document.querySelector("#work > div > div > button");

        calculateButton.addEventListener("click", calculateMortgage);

        function calculateMortgage(){
            PV = borrowAmountValue;
            n = totalYearsValue;
            r = interestRateValue;

            console.log(PV);

            leftSide = r * PV;
            rightSide = Math.pow(1 - (1 + r), n);

            var mortgagePayment = Number(leftSide / rightSide);
        }

    });
</script>

这实际上是我想要的计算:

enter image description here

任何提示都会很棒!

0 个答案:

没有答案