我的代码有什么问题?

软木

我的代码遇到问题,但似乎找不到问题所在。我在浏览器中不断收到相同的错误,但看不到问题出在哪里?

FVOM.js:16未捕获的语法错误:意外的数字

FVOM.html:15未捕获的ReferenceError:未定义CheckSelection

HTML代码:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Future Value Demo</title>
    <link rel="stylesheet" type="text/css" href="css/index.css"/>
    <script type="text/javascript" src="JScripts/FVOM.js"></script>
</head>
<body>
    <table>
        <tr>
            <td colspan="2">
                <input type="radio" id="futVal" name="Money" checked onClick="FVOM:CheckSelection(1)"/>Future Value
                <input type="radio" id="preVal" name="Money" onClick="FVOM:CheckSelection(2)"/>Present Value
                <input type="radio" id="rate" name="Money" onClick="FVOM:CheckSelection(3)"/>Rate of Interest
                <input type="radio" id="time" name="Money" onClick="FVOM:CheckSelection(4)"/>Years
            </td>
        </tr>
    </table>
    <br/>
    <table>
        <tr>
            <td>Future Value (FV)</td>
            <td><input type="text" id="FV" disabled/></td>
        </tr>
        <tr>
            <td>Present Value (PV)</td>
            <td><input type="text" id="PV" value = "1500" /></td>
        </tr>
        <tr>
            <td>Interest Rate [pa] (r)</td>
            <td><input type="text" id="R" value = "4.5"/></td>
        </tr>
        <tr>
            <td>Years (n)</td>
            <td><input type="text" id="N" value = "1"/></td>
        </tr>
        <tr>
            <td>Compounded</td>
            <td>
                <select id="compounded">
                     <option value="year">Yearly</option>
                     <option value="quarter">Quarterly</option>
                     <option value="month">Monthly</option>
                </select>
            </td>
        </tr>
        <tr>
            <td><input type="button" id="reset" value="RESET"/></td>
            <td><input type="button" id="calculate" value="Calculate"/></td>
        </tr>
    </table>
    <br/>
    <hr/>
    <br/>
    <div id="results">

    </div>
</body>

JavaScript:

var $ = function(id){
return document.getElementById(id);
};
var futureValue = function(){
var pv = parseFloat($("PV").value);
var r = parseFloat($("R").value);
var n = parseFloat($("N").value);

if(isNaN(pv) || isNaN(r) || isNaN(n)){
    alert("Please enter a valid number");
}
else{
    r = r/100/Compounded();
    n = n*Compounded();
    $("FV").value=(pv*Math.pow((1+r), n)).toFixed(2);

    var res = "<table border="1"><tr><th>Period</th><th>Investment</th><th>Year End</th></tr>";
    for(var i=1; i<=n; i++){
        res+="<tr><td>"+i+"</td><td>&euro;"+(pv).toFixed(2)+"</td><td>&euro;"+(pv*Math.pow((1+r), 1)).toFixed(2)+"</td></tr>";
        pv = pv*Math.pow((1+r), 1);
    }
    res+="</table>";
    $("results").innerHTML = res;
}
};
var presentValue = function(){
var fv = parseFloat($("FV").value);
var r = parseFloat($("R").value);
var n = parseFloat($("N").value);

if(isNaN(fv) || isNaN(r) || isNaN(n))
    alert("Please enter numbers");
else{
    r = r/100/Compounded();
    n = n*Compounded();
    $("PV").value=(fv/Math.pow((1+r), n)).toFixed(2);
}
};
var rateOfInterest = function(){
var fv = parseFloat($("FV").value);
var pv = parseFloat($("PV").value);
var n = parseFloat($("N").value);

if(isNaN(fv) || isNaN(pv) || isNaN(n))
    alert("Please enter numbers");
else{
    n = n*Compounded();
    $("R").value=((Math.pow((fv/pv),(1/n))-1)*100*Compounded()).toFixed(2)+"%";
}
};
var numberOfYears = function(){
var fv = parseFloat($("FV").value);
var pv = parseFloat($("PV").value);
var r = parseFloat($("R").value);

if(isNaN(fv) || isNaN(pv) || isNaN(r))
    alert("Please enter numbers");
else{
    r = r/100/Compounded;
    $("N").value=(((Math.log(fv)-Math.log(pv))/Math.log(1 + r)))n/Compounded()).toFixed(2);
}
};
var Compounded = function(){
var com = $("compounded").value;
if(com=="year")
    return 1;
if(com=="quarter")
    return 4;
if(com=="month")
    return 12;
};
var calculate = function(){
if($("futVal").checked)
    futureValue();
if($("preVal").checked)
    presentValue();
if($("rate").checked)
    rateOfInterest();
if($("time").checked()
    numberOfYears();
};
var CheckSelection = function(id){
if(id==1){
    $("FV").disabled = true;
    $("PV").disabled = false;
    $("R").disabled = false;
    $("N").disabled = false;
}
else if(id==2){
    $("FV").disabled = false;
    $("PV").disabled = true;
    $("R").disabled = false;
    $("N").disabled = false;
}
else if(id==3){
    $("FV").disabled = false;
    $("PV").disabled = false;
    $("R").disabled = true;
    $("N").disabled = false;
}
else{
    $("FV").disabled = false;
    $("PV").disabled = false;
    $("R").disabled = false;
    $("N").disabled = true;
}
RESET();
};  
var RESET = function(){
$("FV").value = "";
$("PV").value = "";
$("R").value = "";
$("N").value = "";
    $("results").innerHTML = "";
};
window.onload = function(){
$("calculate").onClick = calculate;
$("reset").onClick = RESET;
};

我刚接触JavaScript,因此不胜感激。

墨西哥卷饼
var res = "<table border="1"><tr><th>Period</th><th>Investment</th><th>Year End</th></tr>";

问题出在哪里。您需要使用\“或使用单引号'来使border =” 1“中存在的”(双引号)转义。

因此,有些人在javascript中使用单引号来分配字符串,因此您无需转义双引号。如果您想了解有关javascript中字符串的更多信息,请访问http://www.w3schools.com/js/js_strings.asp,以获取体面的介绍

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章