2013-08-23 46 views
1

有一個基本的javascript興趣計算器,我需要修改計算三個不同的儲蓄計劃之一選擇選擇。Javascript興趣計算器

的基本代碼工作正常:

function CalculateNonComp() { 
if (!parseFloat(document.noncomp.noncomp_principal.value)) { 
    document.noncomp.noncomp_principal.value = 0; 
}; 
if (!parseFloat(document.noncomp.noncomp_interest.value)) { 
    document.noncomp.noncomp_interest.value = 1.4; 
}; 
if (!parseFloat(document.noncomp.noncomp_length.value)) { 
    document.noncomp.noncomp_length.value = 30; 
}; 
var Principal = parseFloat(document.noncomp.noncomp_principal.value); 
var DailyInterest = parseFloat(document.noncomp.noncomp_interest.value)/1; 
DailyInterest = DailyInterest/100; 
var Days = parseInt(document.noncomp.noncomp_length.value) * 1; 
var Result = Principal * (1 + DailyInterest * Days); 
document.getElementById('noncomp_result').innerHTML = Result.toFixed(2); 
} 

我的問題開始當我嘗試實施,如果....否則,如果.... else語句來計算選擇選擇了正確的儲蓄計劃。

我已經試過如下:

function CalculateNonComp() { 
if (document.noncomp.plan.value = "1"); 
if (!parseFloat(document.noncomp.noncomp_principal.value)) { 
    document.noncomp.noncomp_principal.value = 0; 
}; 
if (!parseFloat(document.noncomp.noncomp_interest.value)) { 
    document.noncomp.noncomp_interest.value = 1.4; 
}; 
if (!parseFloat(document.noncomp.noncomp_length.value)) { 
    document.noncomp.noncomp_length.value = 30; 
}; 


var Principal = parseFloat(document.noncomp.noncomp_principal.value); 
var DailyInterest = parseFloat(document.noncomp.noncomp_interest.value)/1; 
DailyInterest = DailyInterest/100; 


var Days = parseInt(document.noncomp.noncomp_length.value) * 1; 

var Result = Principal * (1 + DailyInterest * Days); 

document.getElementById('noncomp_result').innerHTML = Result.toFixed(2); 

else if (document.noncomp.plan.value = "2"); 


if (!parseFloat(document.noncomp.noncomp_principal.value)) { 
    document.noncomp.noncomp_principal.value = 0; 
}; 
if (!parseFloat(document.noncomp.noncomp_interest.value)) { 
    document.noncomp.noncomp_interest.value = 1.6; 
}; 
if (!parseFloat(document.noncomp.noncomp_length.value)) { 
    document.noncomp.noncomp_length.value = 60; 
}; 


var Principal = parseFloat(document.noncomp.noncomp_principal.value); 
var DailyInterest = parseFloat(document.noncomp.noncomp_interest.value)/1; 
DailyInterest = DailyInterest/100; 


var Days = parseInt(document.noncomp.noncomp_length.value) * 1; 

var Result = Principal * (1 + DailyInterest * Days); 

document.getElementById('noncomp_result').innerHTML = Result.toFixed(2); 

else(document.noncomp.plan.value = "3"); 


if (!parseFloat(document.noncomp.noncomp_principal.value)) { 
    document.noncomp.noncomp_principal.value = 0; 
}; 
if (!parseFloat(document.noncomp.noncomp_interest.value)) { 
    document.noncomp.noncomp_interest.value = 1.8; 
}; 
if (!parseFloat(document.noncomp.noncomp_length.value)) { 
    document.noncomp.noncomp_length.value = 90; 
}; 


var Principal = parseFloat(document.noncomp.noncomp_principal.value); 
var DailyInterest = parseFloat(document.noncomp.noncomp_interest.value)/1; 
DailyInterest = DailyInterest/100; 


var Days = parseInt(document.noncomp.noncomp_length.value) * 1; 

var Result = Principal * (1 + DailyInterest * Days); 

document.getElementById('noncomp_result').innerHTML = Result.toFixed(2); 

} 

感謝任何提示或幫助!

+0

檢查if else語句的語法 – ianace

+0

['else'](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else)必須緊跟在後面'if'。 – Teemu

+0

我的問題是,我不是一個程序員,通常只是使用準備爲我的客戶項目使用js。 –

回答

0

。如果正確使用..其它if .. else語句是:

if (condition) { 
    // do something 
} else if (condition) { 
    // do something 
} else { 
    // do something 
} 

Comp這是對你的代碼,並認爲你做了什麼錯]

+0

感謝Jakub,我修改了代碼,至少我沒有再收到任何js錯誤。然而,只要選擇其他條件,計算器只會計算條件1。我猜如果(document.noncomp.plan.value =「1」),否則如果(document.noncomp.plan.value =「2」)不正確以從select中獲取值。 –

+0

在您的條件中將「==」替換爲「=」: document.noncomp.plan.value ==「1」等 –

+0

再次感謝:)問題幾乎解決了。現在,當我計算任何樹選項時,腳本正在做什麼預期的。但是,如果在計算之後我將選擇更改爲另一個,那麼腳本正在計算以前的選擇。我需要重新加載整個頁面來計算另一個選擇。有沒有辦法重置腳本或什麼? –

1

你需要用 '{' 和 '}'

if(condition 1) { 
code 
} else if() { 

} 
-2

我嘗試使用Java腳本來計算與表格和文本框驗證所有輸入驗證簡單的利益,也有應用CSS

  <html> 

     <head> 
     <title>SIMPLE INTEREST CALCULATION</title> 

     <link type="text/css" href="style.css" rel="stylesheet "> 

     <script type="text/javascript" src="style.js"> 

     </script> 

     </head> 

     <body> 

    <form name="frm"> 

    <center> 

    <h1>....Simple Interest Calculation....</h1> 

    <div id="div"> 

    <table id="t1"> 

    <caption> </caption> 

    <tr> 
    <td>Amount Rs/- 
    </td> 

    <td> 

    <input type="text" name="amount" id="amount" value="" class="input" min="0" onkeyup="AllowNumber()"/> 
     </td> 
    </tr> 

     <tr> 
     <td>Rate Rs/- 
    </td> 

     <td><input type="text" name="rate" id="per" value="" min="0" max="3"  class="input" /> 
    </td> 
    </tr> 

    <tr> 
    <td>Time peroid:</td> 
    <td> 
    <input type="number" name="time" value="" id="time" min="0" class="input"/> 

    <select id="peroid" class="choice"> 

    <option>Choose</option> 

    <option value="year">Year</option> 

    <option value="month">Month</option> 

    <option value="weeks">Weeks</option> 

    <option value="days">Days</option> 

    </select> 

    </td> 

    </tr> 

    <tr> 

    <td> 
Total Interest: 
    </td> 

    <td> 
    <input type="text" id="interest" value="" name="interest" class="input" readonly/> 
    </td> 
    </tr> 

    <tr><td>TotalAmount:</td> 
    <td><input type="text" id="total" name="total" value="" class="input" readonly/></td></tr> 

    <tr> 
    <td><input type="button" name="cal" id="cal" value="Calculate" class="cal" onclick="cali()"/> 
    </td> 

    <td><input type="reset" name="clear" id="cal" value="Clear" class="cal"/> </td></tr> 
    </table> 
    </center> 
    </div> 
    </form> 
    </body> 
    </html> 


    **CSS Code**//style.css 

    .input 
    { 
    font-weight:bold; 
    width:260px; 
    height:32px; 
    font-size:1em; 
    text-align: right 
    } 

    #amount 
    { 
    border-radius: 40px; 
    border:2px solid teal; 
    } 
    #per 
    { 
    border-radius: 40px; 
    border:2px solid teal; 
    } 

    #time 
    { 
    border-radius: 40px; 
    border:2px solid teal; 
    width:130px; 
    height:30px; 
    } 

    #interest 
    { 
    border-radius: 40px; 
    border:2px solid teal; 
    } 
    #total 
    { 
    border-radius: 40px; 
    border:2px solid teal; 
    } 
    .cal 
    { 
    font-weight:bold; 
    width:250px; 
    height:40px; 
    font-size:1em; 
    border-radius: 50px; 
    border:2px solid black; 
    } 
    #t1 
    { 
    color:teal; 
    font-size:2em; 
    border-radius: 40px; 
    font-weight:bold; 

    font-family:Gabriola; 
    } 

    select 
    { 
    border-radius: 40px; 
    border:2px solid teal; 
    width:130px; 
    height:35px; 
    font-size:1em; 
    } 

    #div 
    { 
    background-color: lightgrey; 
    width: 600px; 
    padding: 20px; 
    border: 25px solid green; 
    margin: 20px; 
    border-style: inset; 
    border-radius: 50px; 
    box-shadow: 10px 10px 5px red; 
    } 
    h1 
    { 
    color:purple; 
    hypens:auto; 
    text-shadow: 10px 10px 10px black; 
    font-weight:bold; 
    font-variant: small-caps; 
    font-size:3em; 
    } 
    #d1 
{ 
    background-color: lightgrey; 
    width: 850px; 
    padding: 25px; 
    border: 25px solid navy; 
    margin:60px; 
    margn-left:5px; 
    } 

    #cal 
    { 
    background-color:teal; 
    width:280px;!important 
    height:33px;!important 
    } 

    **JavaScript Code**//style.js 

    //Empty text box validation 

function cali() 
{ 
    var p,r,n,y,m,w,d; 
    if(document.frm.amount.value == "") 
    { 
    alert("PLEASE ENTER PRINCIPLE AMOUNT"); 
    document.frm.amount.alert(); 
    } 
    else if(document.frm.rate.value=="") 
    { 
    alert("ENTER RATE OF INTEREST"); 
    document.frm.rate.blur(); 
    } 

    if(parseFloat(per.value)>100)//text box allow only range of within 100 
    { 
    alert("Please Enter The Interest Rate Less Then 100"); 
    per[i].focus(); 
    return (false); 
    } 

    if(parseFloat(per.value)<0)//text box allow only range of Greater Than Zero 
    { 
    alert("Please Enter The Interest Rate Greater then 0"); 
    per[i].focus(); 
    return (false); 
    } 

    if(!document.getElementById("per").value.match(/^[0-9]*\.?[0-9]*$/))//text box allow only numbers and allow only one dot 
    { 
    alert("Enter Correct Interest Rate"); 
    return (false); 
    } 
    else if(document.frm.time.value=="") 
    { 
    alert("ENTER NUMBER OF TIME PERIOD"); 
    document.frm.time.prompt(); 
    } 
    else 
    { 
    p=parseInt(document.frm.amount.value); 

    r=parseFloat(document.frm.rate.value); 

    n=parseInt(document.frm.time.value); 

    peroid=document.getElementById("peroid").value; 

    if(peroid=="year")// per year calculation 
    { 
    y=(p*r*n)/100; 
    document.frm.interest.value="Rs :" +Math.ceil(y); 
    } 

    else if(peroid=="month")//per month calculation 
    { 
    m=(p*r*(n/12))/100; 
    document.frm.interest.value="Rs :" +Math.ceil(m); 
    } 

    else if(peroid=="weeks")//per week calculation 
    { 
    var w=(p*r*(n/52))/100; 
    document.frm.interest.value="Rs :" + Math.ceil(w); 
    } 


    else if(peroid=="days")//per days calculation 
    { 
    d=(p*r*(n/365))/100; 
    document.frm.interest.value="Rs :" + Math.ceil(d); 
    } 

    document.frm.total.value="Rs:"+Math.ceil((p+m)||(p+y)||(p+w)||(p+d)); 
    } 
    } 

    //Text Box Allow Only The Numeric Values not allow single dot or char 

    function AllowNumber(){ 
    if (!frm.amount.value.match(/[0-9]+$/) && frm.amount.value !="") 
    { 
     frm.amount.value=""; 
    frm.amount.focus(); 
    alert("Please Enter only Numers In This Field"); 
    } 
    } 
+0

此答案與原始海報問題不符。這個問題與JavaScript有關,你只發布了一些與問題無關的HTML。 –