信用计算器不起作用

时间:2018-07-30 22:18:54

标签: javascript

我在课堂上创建了一个基本的学分计算器,但我一直坚持为什么不调用我的函数。我添加了一个警报,以查看它是否甚至可以使它进入功能,并且它无法正常工作……我确定它有点小,但是在盯着屏幕看了几个小时之后,我的教授却无法看到为什么不起作用。我可以帮忙。

<!doctype html>
<html lang="en">
<head>
<title>Credit Score Grant or Deny</title>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<!-- jQuery library -->
<script 
src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> 
</script>

<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"</script>
<script src="separate.js"></script>

<script>
$(document).ready(function() {

   $("#creditcalc").click( function() {
        alert ("YOLO")
       var input = $("#creditscore").val();
       if (isNaN(input))
            $("#output").html ("Please enter a valid 
             number!")

            else if (parseFloat (input) > 850 || parseFloat 
            (input) < 350) 
            $("#output").html ("Please enter a valid credit 
            score between 350 and 850!")

            else if (parseFloat (input) >= 780 && parseFloat 
            (input) <= 850)
            $("#output").html ("We will grant you a loan at a 
            5% interest rate with your credit score of " + 
            (input))

            else if (parseFloat (input) >= 720 && parseFloat 
            (input) <= 779)
            $("#output").html ("We will grant you a loan at a 
            7% interest rate with your credit score of " + 
            (input))

            else if (parseFloat (input) >= 680 && parseFloat 
            (input) <= 719)
            $("#output").html ("We will grant you a loan at a 
            10% interest rate with your credit score of " + 
            (input))

            else if (parseFloat (input) >= 620 && parseFloat 
            (input) <= 679)
            $("#output").html ("We will grant you a loan at a 
            15% interest rate with your credit score of " + 
            (input))

            else if (parseFloat (input) >= 560 && parseFloat 
            (input) <= 619)
            $("#output").html ("We will grant you a loan at a 
            24% interest rate with your credit score of " + 
            (input))

            else
            $("#output").html ("You are denied. We cannot 
            loan you the money with your low credit score of 
            " + (input) + " at this time. Please work on 
            improving your score.")

   });      
});

</script>

 </head>

<body>

 <center><h2>Credit Score Determination</h2> </center>
<form>
    <center><p>Please enter your credit score and we will let 
 you know if we will grant you a loan and what rate you 
 qualify at!</p></center> 
<br/>
<center><input type="text" id="creditscore"></center>
<br/>
<center><input type="button" id="creditcalc" value="Click 
here to see if you qualify!"></center>
</form>
<br/>
<center><p id="output">I will let you know if your credit 
score qualifies you after you enter a number and click the 
 button!</p></center>

 </body>
 </html>

2 个答案:

答案 0 :(得分:1)

使用input时,您需要先使用parseIntisNaN的值转换为数字,因为inputtextarea的值是始终是String

主要问题是您的字符串跨越多行,这是语法错误。您必须将它们与+串联在一起,或者将它们保持在一行上。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<center><h2>Credit Score Determination</h2> </center>
<form>
    <center><p>Please enter your credit score and we will let 
 you know if we will grant you a loan and what rate you 
 qualify at!</p></center> 
<br/>
<center><input type="text" id="creditscore"></center>
<br/>
<center><input type="button" id="creditcalc" value="Click 
here to see if you qualify!"></center>
</form>
<br/>
<center><p id="output">I will let you know if your credit 
score qualifies you after you enter a number and click the 
 button!</p></center>
 <script>
$(document).ready(function() {

   $("#creditcalc").click( function() {
        alert ("YOLO")
       var input = $("#creditscore").val();
       if (isNaN(parseInt(input)))
            $("#output").html ("Please enter a valid              number!")

            else if (parseFloat (input) > 850 || parseFloat 
            (input) < 350) 
            $("#output").html ("Please enter a valid credit"+
            "score between 350 and 850!")

            else if (parseFloat (input) >= 780 && parseFloat 
            (input) <= 850)
            $("#output").html ("We will grant you a loan at a "+
            "5% interest rate with your credit score of " + 
            (input))

            else if (parseFloat (input) >= 720 && parseFloat 
            (input) <= 779)
            $("#output").html ("We will grant you a loan at a"+ 
           " 7% interest rate with your credit score of " + 
            (input))

            else if (parseFloat (input) >= 680 && parseFloat 
            (input) <= 719)
            $("#output").html ("We will grant you a loan at a"+ 
           " 10% interest rate with your credit score of " + 
            (input))

            else if (parseFloat (input) >= 620 && parseFloat 
            (input) <= 679)
            $("#output").html ("We will grant you a loan at a"+ 
           " 15% interest rate with your credit score of " + 
            (input))

            else if (parseFloat (input) >= 560 && parseFloat 
            (input) <= 619)
            $("#output").html ("We will grant you a loan at a "+
            "24% interest rate with your credit score of " + 
            (input))

            else
            $("#output").html ("You are denied. We cannot "+
            "loan you the money with your low credit score of "     + (input) + " at this time. Please work on "+
            "improving your score.")

   });      
});

</script>

答案 1 :(得分:-1)

您的字符串必须在它们开始的同一行结束。 (您可以将一串字符串固定在一起,但是不能像您一样随意打开字符串。)摆脱字符串中间的换行符,或者执行类似的操作

        $("#output").html ("We will grant you a loan at a " + 
        "24% interest rate with your credit score of " + 
        (input))