2012-10-21 122 views
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

回答

1

在部分:

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

請注意,如果該文檔已完成加載,以document.write通話將首先調用document.open,它清除的全部內容文檔(包括所有腳本和HTML元素本身)。

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

那 「撥弄」 完全不爲我工作。最好將代碼減少到顯示問題併發布該問題的絕對最小值。這個練習可能會導致你的問題。

它可能會幫助您的調查知道parseInt('')返回NaN