提问人:Webmaster website 提问时间:6/18/2023 更新时间:6/18/2023 访问量:29
if else 语句在 HTML 中无法正常工作
if else statement not working correctly in html
问:
我正在为非营利组织开发网页,但该网页的 if else 语句无法正常工作。我不是最好的程序员,不确定我做错了什么。该网页应该要求在文本框中输入邮政编码,然后将该人发送到与该邮政编码关联的正确外部网页。目前,它不会转到外部网页,甚至不会显示 else 语句。所以我在 if else 语句中做错了什么。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Find your nearest Meeting </title>
</head>
<body>
<h1><b>Finding your nearest meeting</b></h1>
<p>When leaving your treatment center and going back home, it is importaint to find your nearest AA meeting as soon as possible.<br>
The Area 26 Treatment Committee wants to make this easy for you.
Please type in your zipcode <b><i>OR</i></b> your city <b><i>OR</i></b> your county in <i><u>one</u></i> of the boxes below.<br>
You will then be directed to the Intergroup meeting webpage that serves that area.
Here you will be able to locate a meeting and continue your journey in sobrity.<br>
Remeber, <i>We shall be with you in the Fellowship of the Spirit, and you will surely meet some of us as you trudge the
Road of Happy Destiny.</i>
</p>
<br>
<label for="Zipcode">Zipcode:</label>
<input type="text" id="Zipcode" name="Zipcode">
<p id="Zipcode"></p>
<br><br><br>
<button onclick="myFunction()">Check</button>
<script>
function myFunction()
{
var a=document.getElementById("Zipcode").value;
if(a=="40342") {
document.getElementById("Zipcode").innerHTML="Thank you. You will be directed to the Bluegrass Intergroup’s Meeting page";
<a href="https://www.bluegrassintergroup.org/meetings/"> Bluegrass Intergroup Meeting Page</a>;
window.open(https://www.bluegrassintergroup.org/meetings/, ‘newwindow’)
}
else if(a=="40358")
{
document.getElementById("Zipcode").innerHTML="Thank you. You will be directed to the Bluegrass Intergroup’s Meeting page";
<a href="https://www.bluegrassintergroup.org/meetings/"> Bluegrass Intergroup Meeting Page</a>;
window.open(https://www.bluegrassintergroup.org/meetings/, ‘newwindow’)
}
else
{
document.getElementById("Zipcode").innerHTML="I'm sorry but we have not found an Intergroup Meeting Page that has meetings in that area. Please go to the Area 26 meeting page for a list of meetings in Kentucky, Southern Indiana and Ohio.";
<a href="https://www.area26.net/wp/?post_type=tsml_meeting"> Area 26 Find A Meeting Page</a>;
window.open(https://www.area26.net/wp/?post_type=tsml_meeting, ‘newwindow’)
答:
-2赞
X-_-FARZA_ D-_-X
6/18/2023
#1
不将字符串放在引号“string”中
不关闭最后一个 else 语句的结束大括号
没有关闭 </script> </body> </html> 标签
使用 ' 而不是 '
固定版本:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Find your nearest Meeting </title>
</head>
<body>
<h1><b>Finding your nearest meeting</b></h1>
<p>When leaving your treatment center and going back home, it is importaint to find your nearest AA meeting as soon
as possible.<br>
The Area 26 Treatment Committee wants to make this easy for you.
Please type in your zipcode <b><i>OR</i></b> your city <b><i>OR</i></b> your county in <i><u>one</u></i> of the
boxes below.<br>
You will then be directed to the Intergroup meeting webpage that serves that area.
Here you will be able to locate a meeting and continue your journey in sobrity.<br>
Remeber, <i>We shall be with you in the Fellowship of the Spirit, and you will surely meet some of us as you
trudge the
Road of Happy Destiny.</i>
</p>
<br>
<label for="Zipcode">Zipcode:</label>
<input type="text" id="Zipcode" name="Zipcode">
<p id="Zipcode_text"></p>
<br><br><br>
<button onclick="myFunction()">Check</button>
<script>
function myFunction() {
var a = document.getElementById("Zipcode").value;
if (a == "40342") {
document.getElementById("Zipcode_text").innerHTML = "Thank you. You will be directed to the Bluegrass Intergroup’s Meeting page";
// <a href="https://www.bluegrassintergroup.org/meetings/"> Bluegrass Intergroup Meeting Page</a>;
window.open("https://www.bluegrassintergroup.org/meetings/", 'newwindow')
}
else if (a == "40358") {
document.getElementById("Zipcode_text").innerHTML = "Thank you. You will be directed to the Bluegrass Intergroup’s Meeting page";
// <a href="https://www.bluegrassintergroup.org/meetings/"> Bluegrass Intergroup Meeting Page</a>;
window.open("https://www.bluegrassintergroup.org/meetings/", 'newwindow')
}
else {
document.getElementById("Zipcode_text").innerHTML = "I'm sorry but we have not found an Intergroup Meeting Page that has meetings in that area. Please go to the Area 26 meeting page for a list of meetings in Kentucky, Southern Indiana and Ohio.";
// <a href="https://www.area26.net/wp/?post_type=tsml_meeting"> Area 26 Find A Meeting Page</a>;
window.open("https://www.area26.net/wp/?post_type=tsml_meeting", 'newwindow')
}
}
</script>
</body>
</html>
评论
<a href...></a>
"
window.open("https://...", "_blank")
b
h1
br
p
output
if/else in your cases. In modern JS you should use external JS and use event listeners over the outdated