if else 语句在 HTML 中无法正常工作

if else statement not working correctly in html

提问人:Webmaster website 提问时间:6/18/2023 更新时间:6/18/2023 访问量:29

问:

我正在为非营利组织开发网页,但该网页的 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’)
HTML IF语句

评论

0赞 Icepickle 6/18/2023
为什么要将 HTML 与 JavaScript 代码混合使用?从代码中删除内容,并在 .如果您查看开发人员控制台,则在加载当前页面时应该会看到语法错误:)<a href...></a>"window.open("https://...", "_blank")
0赞 tacoshy 6/18/2023
尽管有错别字,但还是有很多问题。 里面没有意义。与其使用,不如读入边距。BR 是将流文本中断到下一行,而不是创建具有填充和边距的视觉间隙。出于性能和安全原因,innerHTML 的使用已经过时。ID 必须是唯一的,并且不能多次使用。a 不能替代 .您应该读入 switch 语句,而不是使用 onclick' 属性。bh1brpoutputif/else in your cases. In modern JS you should use external JS and use event listeners over the outdated

答:

-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>