[英]Javascript function is not returning the value
我是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>
第一個錯誤在此行上:
<input type="button" name="toClick" value="calculate" onclick="grossPts.value
= calcpts(points)" >
沒有聲明points
變量,因此會引發錯誤,您需要計算它或傳遞一個數字,無論如何您都不會在函數內部使用該變量。
第二個錯誤是parseint()
不存在,將其更改為parseInt()
第三個錯誤是您的函數內部,您使用了不存在的pay
變量,因此需要聲明變量才能使用它們。 但是我想你是要寫pts
而不是pay
這是禮物,為什么不呢?
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.