location.href 将“”添加到文件名

location.href adds "" to file name

这是我的代码:

<!DOCTYPE html>
<html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<style>
    #wrap { 
        width: 1000px; 
        margin: 0 auto; 
    }
    .out_box {
        float: left;
        margin: 20px 20px 20px 20px;
        border: 1px solid black;
        padding: 0 5px 0 5px;
        min-width: 280px;
    }
    input {
        margin: 10px;
    }
    input {
        vertical-align: -3px;
    }
    h1 {
        font-size: 400%;
        color: black;
        margin: 0 0 10px 0;
    text-align: center;
    }
    h2 {
        font-size: 100%;
        color: black;
        margin: 5px 0 5px 0;
    text-align: center;
    }
    h3 {
        font-size: 95%;
        color: black;
        margin: 5px 0 5px 0;
    }
    h4 {
        font-size: 200%;
        color: black;
        margin: 5px 0 5px 0;
    text-align: center;
    }
    p, form, button {
        font-size: 80%;
        color: #252525;
    }
    .small_text {
        font-size: 70%;
        color: #737373;
    }
    body {
        background-color: lightblue;
    }
</style>
</head>
<body>
    <div id=wrap>
        <h1>Login</h1>
        <form class="form1" action=”index3.htm”>
            <div class="formtitle">
                Enter the password to proceed
            </div>

            <div class="input nobottomborder">
                <div class="inputtext">
                    Password:
                </div>

                <div class="inputcontent">
                    <input type="password" id="password" /><br />
                </div>
            </div>

            <div class="buttons">
                <input class="orangebutton" type="submit" value="Login" onclick="if (document.getElementById('password').value == ’smurfsmurf’) location.href='index3.htm'; else alert('Wrong Password!');" />
            </div>
        </form>
    </div>
</body>
</html>

当我让它重定向时,它的地址是这样的:file:///Volumes/ETHERNET/"index3.htm"

该目录中有一个名为 index.htm 的文件,这就是我要访问的文件。我该如何修复它添加“”符号?

此致

奥斯卡

您的表单操作中的引号类型有误。更改此行:

<form class="form1" action=”index3.htm”>

为此:

<form class="form1" action="index3.htm">

此外,由于您的 javascript 位于一个按钮上,根据标准,该按钮将提交表单,因此您的 javascript 不会像您预期的那样做出反应。您应该在代码中添加 return false; ,并在 if 语句周围添加大括号,并删除弯曲的单引号:

<input class="orangebutton" type="submit" value="Login" onclick="if (document.getElementById('password').value == 'smurfsmurf') {location.href='index3.htm';} else {alert('Wrong Password!');}return false;" />

尽管就我个人而言,我建议不要像那样内联 JavaScripting。相反,请使用 EventTarget.addEventListenerhttps://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener 向下滚动到示例以查看其工作原理。

解释:

直引号 技术上 不是 HTML 属性值所必需的(无论如何我强烈建议使用它们)。您没有使用直引号 ("),而是使用了弯引号 (),这对于 HTML 属性值的包装是无效的。因此,浏览器将表单上的 action 解释为 ”index3.htm” 而不是 index3.htm.

当您单击 submit 按钮时,默认操作是将表单提交给它的 action 属性。所以你的浏览器被重定向到 /”index3.htm”。在您被重定向到表单的操作值之前,您的 JavaScript 没有时间启动和更改浏览器的位置。

让我知道这个解释是否成立。