是js的新手,并且希望使用我的js“ if”代码中输入的值。我在下面编写了代码,但未呈现。你能告诉我我做错了吗。
function myFunction() {
var age = document.getElementById('selection').value;
if (age < 4) {
alert('The child is too young');
} else if (age > 40) {
alert('You are too old for this course');
}
}
<form onsubmit=" myFunction()">
Age: <input type="text" id="selection" name="age">
<input type="submit" value="submit">
</form>
答案 0 :(得分:0)
你非常亲近。
这应该工作
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>js loops</title>
<link rel="stylesheet" href="loops.css">
<script>
function myFunction() {
var age = document.getElementById('selection').value;
if (age < 4) {
alert('The child is too young');
} else if (age > 40) {
alert('You are too old for this course');
}
return false;
}
</script>
</head>
<body>
<form onsubmit="return myFunction()">
Age: <input type="text" id="selection" name="age">
<input type="submit" value="submit">
</form>
</body>
</html>