0

查看我的代码

它有效,但 jsfiddle 出于某种原因讨厌它。但是当它在浏览器中运行时,我得到NaN了计算

出于某种原因,无论解析如何,它都不会返回一个integer来执行计算。

有人知道为什么吗?还

// JavaScript Document
var payment

/* requirement #2* Each input (years, loan amount, interest rate)
   will have its own number pad for entry */
function getNum(id,span) {
    var a;
    a = parseInt(document.getElementById(id).value);
    document.getElementById(span).innerHTML += a;
}

function clear1(span) {
    document.getElementById(span).innerHTML = "";
}

/* requirment #7 Mortgage object with three variables:  years, amount, rate */
function Mortgage(years, amount, rate) {
    this.years = years;
    this.amount = amount;
    this.rate = rate;
    /*Requirment #8. Object must have an internal function that resets all
      values/variables to default and clears amounts displayed to user */
    this.clearAll = function() {
        document.getElementById(years).innerHTML = "";
        document.getElementById(amount).innerHTML = "";
        document.getElementById(rate).innerHTML = "";
    }
    /*gets the mortgage from spans*/                        
    this.getCalc = function() {
        /*Requirment # 9 Object must call at least 1 external function */
        get();
    }
}

function test() {          
    /*uses the params to call the spans id*/                     
    var c = new Mortgage('yInput','lInput','rInput');
    c.clearAll();
}

/* an external cunction to calculate mortgage*/
function get() {
    var m = new Mortgage(parseInt(document.getElementById('yInput').innerHTML),
                         parseInt(document.getElementById('lInput').innerHTML),
                         parseInt(document.getElementById('rInput').innerHTML)
    );

    /* this is NaN?*/
    document.write(m.years-m.rate);
}

function calculate() {
    var c = new Mortgage();
    c.getCalc();
}

http://jsfiddle.net/5qf7f/6/#run

4

1 回答 1

1

在部分:

> /* this is NaN?*/
> document.write(m.years-m.rate);

请注意,如果文档已完成加载,document.write将首先调用document.open,这将清除文档的全部内容(包括所有脚本和 HTML 元素本身)。

http://jsfiddle.net/5qf7f/6/#run

那个“小提琴”对我根本不起作用。最好将您的代码减少到显示问题并发布的绝对最小值。该练习可能会导致您遇到问题。

知道parseInt('')返回可能有助于您的调查NaN

于 2012-10-21T23:35:44.537 回答