Javascript 在我的项目中使用.preventDefault时出现问题
我正在尝试为我正在进行的一个基本项目构建一个测试运行 我希望用户使用这两个表单输入他们的出生日期和月份,并返回他们的星号。 在我尝试使用.preventDefault之前,它似乎一直在工作。它似乎仍在试图翻开另一页,我不知道哪里出了错 这是代码,请随意评论我,谢谢Javascript 在我的项目中使用.preventDefault时出现问题,javascript,html,Javascript,Html,我正在尝试为我正在进行的一个基本项目构建一个测试运行 我希望用户使用这两个表单输入他们的出生日期和月份,并返回他们的星号。 在我尝试使用.preventDefault之前,它似乎一直在工作。它似乎仍在试图翻开另一页,我不知道哪里出了错 这是代码,请随意评论我,谢谢 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" con
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>Starsign Calc.</title>
</head>
<body>
<form method="get">
<h3>Enter Your Month and Day of Birth</h3>
<select name="usermonth" id="usermonth">
<option value="january">January</option>
<option value="febuary">Febuary</option>
<option value="march">March</option>
<option value="april">April</option>
</select>
<input name="userday" id="userday" type="number" min="1" max="31" value="1" />
<input id="change-placeholder" type="submit" />
</form>
<hr />
<h1>You are a ???????</h1>
<script type="text/javascript">
document.getElementById('change-placeholder').addEventListener('click', function(e) {
e.preventDefault();
let birthMonth = document.getElementById('usermonth').value;
let birthDay = document.getElementById('userday').value;
//is user a Pisces?
if (birthMonth === 'febuary' && birthDay >= 19 && birthDay <= 28) {
document.querySelector('h1').innerHTML = "You are a Pisces";
} else if (userMonth === 'march' && userDay <= 20) {
document.querySelector('h1').innerHTML = "You are a Pisces";
}
};
</script>
</body>
</html>
星号计算。
输入您的出生月份和日期
一月
二月
前进
四月
你是一个???????
document.getElementById('change-placeholder')。addEventListener('click',函数(e){
e、 预防默认值();
让birthMonth=document.getElementById('usermonth').value;
让生日=document.getElementById('userday').value;
//用户是双鱼座的吗?
如果(birthMonth==='febuary'&&birth>=19&&birth您在}
之后的脚本结尾处未关闭。这是第一个语法错误,需要更正
单击按钮会出现以下错误:
Uncaught ReferenceError: userMonth is not defined
at HTMLInputElement.<anonymous> (<anonymous>:12:18)
最后,如果没有匹配项,则需要一个finalelse
语句:
if (birthMonth === "febuary" && birthDay >= 19 && birthDay <= 28) {
document.querySelector("h1").innerHTML = "You are a Pisces";
} else if (birthMonth === "march" && birthDay <= 20) {
document.querySelector("h1").innerHTML = "You are a Pisces";
} else {
document.querySelector("h1").innerHTML = "I am not sure...";
}
if(birthMonth===“febuary”&&birth>=19&&birth>改为收听表单提交事件-或者不使用表单-但如果代码无效,并且使用未声明的变量,则没有帮助
if (birthMonth === "febuary" && birthDay >= 19 && birthDay <= 28) {
document.querySelector("h1").innerHTML = "You are a Pisces";
} else if (birthMonth === "march" && birthDay <= 20) {
document.querySelector("h1").innerHTML = "You are a Pisces";
} else {
document.querySelector("h1").innerHTML = "I am not sure...";
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>Starsign Calc.</title>
</head>
<body>
<form method="get">
<h3>Enter Your Month and Day of Birth</h3>
<select name="usermonth" id="usermonth">
<option value="january">January</option>
<option value="febuary">Febuary</option>
<option value="march">March</option>
<option value="april">April</option>
</select>
<input name="userday" id="userday" type="number" min="1" max="31" value="1" />
<input id="change-placeholder" type="submit" />
</form>
<hr />
<h1>You are a ???????</h1>
<script type="text/javascript">
document
.getElementById("change-placeholder")
.addEventListener("click", function (e) {
e.preventDefault();
let birthMonth = document.getElementById("usermonth").value;
let birthDay = document.getElementById("userday").value;
//is user a Pisces?
if (birthMonth === "febuary" && birthDay >= 19 && birthDay <= 28) {
document.querySelector("h1").innerHTML = "You are a Pisces";
} else if (birthMonth === "march" && birthDay <= 20) {
document.querySelector("h1").innerHTML = "You are a Pisces";
} else {
document.querySelector("h1").innerHTML = "I am not sure...";
}
});
</script>
</body>
</html>