简体   繁体   English

Javascript函数未返回值

[英]Javascript function is not returning the value

I am new to JS and taking a online class, I cant determine why this function does not return the intended value of 20. 我是JS的新手,正在参加在线课程,我无法确定为什么此函数不返回预期值20。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script type = "text/javascript">
function calcpts(points)
{
    if (window.document.jsquiz.rad1[0].checked == true)
    {
        var pts = 0;
        pts = pts + parseint(20);
        alert(pay);
        return pay;
    }
}
</script>
</head>
<body>
<h1>JavaScript Quiz</h1>
<form name = "jsquiz">
    <p>Question #1 You can test a condition with an if..else statement or with  an if..elseif..else statement</p>
    <input type = "radio" name = "rad1" value="ques">True<br>
    <input type = "radio" name = "rad1" value="ques">False<br><br>
    <input type="button" name="toClick" value="calculate" onclick="grossPts.value = calcpts(points)" >
    </p>
    <br>
    <p>Your total points are: <input type="text" name="grossPts" size="5" /></p>
</form>
</body>
</html>

The first error is on this line: 第一个错误在此行上:

<input type="button" name="toClick" value="calculate"       onclick="grossPts.value 
= calcpts(points)" >

there is not a points variable declared so it throws an error, you need to calculate it or pass a number, you're not using the variable inside your function anyways. 没有声明points变量,因此会引发错误,您需要计算它或传递一个数字,无论如何您都不会在函数内部使用该变量。

The second error is that parseint() does not exist, change it for parseInt() 第二个错误是parseint()不存在,将其更改为parseInt()

A third error is inside your function, your using a pay variable that does not exist, you need to declare the variables in order to be able to use them. 第三个错误是您的函数内部,您使用了不存在的pay变量,因此需要声明变量才能使用它们。 But i guess you meant to write pts instead of pay 但是我想你是要写pts而不是pay

Here's a gift, because why not?: 这是礼物,为什么不呢?

  function calcpts(points) { if (window.document.jsquiz.rad1[0].checked == true) { var pts = 0; pts = pts + parseInt(20); alert(pts); return pts; } } 
  <h1>JavaScript Quiz</h1> <form name = "jsquiz"> <p>Question #1 You can test a condition with an if..else statement or with an if..elseif..else statement</p> <input type = "radio" name = "rad1" value="ques">True<br> <input type = "radio" name = "rad1" value="ques">False<br><br> <input type="button" name="toClick" value="calculate" onclick="grossPts.value = calcpts(0)" > </p> <br> <p>Your total points are: <input type="text" name="grossPts" size="5" /></p> </form> 

在代码中用parseInt替换函数parseint

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM