从跨度中获取价值

时间:2012-10-21 22:22:58

标签: javascript html calculator

查看我的代码

它有效但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

1 个答案:

答案 0 :(得分:1)

在部分中:

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

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

  

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

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

了解parseInt('')返回NaN可能有助于您的调查。