我对javascript并不陌生,我正在尝试创建一个函数来告诉给定的年份是否为a年。我想从form>input
中获取价值
请纠正我在哪里弄错了。
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="javascript.js"></script>
</head>
<body>
<h3 style="text-transform: capitalize;">please enter the current year to check the leap year</h3>
<form>
Current Year:<br>
<input type="text" name="years" value="2018"><br>
</form>
<button style="padding: 5px 10px; background-color:aqua; font-weight: bold;
margin: 20px;" onclick="hello()">Let's go!</button>
<p id="leap"></p>
</body>
</html>
javascript.js
function hello(){
var leapYear = document.forms.years.value;
var numb = parseInt(leapYear, 10);
var result = numb % 4;
if (result === 0){
document.getElementById("leap").innerHTML= "Yes, This Is A Leap Year"
}
else(
document.getElementById("leap").innerHTML= "No, This Is Not A Leap Year"
);
}
答案 0 :(得分:0)
您去了:阅读评论,看看我做了什么更改。
function hello(){
var leapYear = document.getElementById("leapInput").value;
//changed value of leapYear from "document.forms.years.value" to document.getElementById("leapInput").value
var numb = parseInt(leapYear, 10);
var result = numb % 4;
if (result === 0){
document.getElementById("leap").innerHTML= "Yes, This Is A Leap Year"
}
else(
document.getElementById("leap").innerHTML= "No, This Is Not A Leap Year"
);
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="javascript.js"></script>
</head>
<body>
<h3 style="text-transform: capitalize;">please enter the current year to check the leap year</h3>
<form>
Current Year:<br>
<input id="leapInput" type="text" name="years" value="2018"><br>
<!--Added id of "leapInput" to the <input> above-->
</form>
<button style="padding: 5px 10px; background-color:aqua; font-weight: bold;
margin: 20px;" onclick="hello()">Let's go!</button>
<p id="leap"></p>
</body>
</html>
并且,当您在此网站上提问时,请将代码发布到问题本身中。如果由于某种原因无法使用网站CodePen。用您的代码创建一支笔,然后将其链接到问题。