Javascript 试图了解表单如何工作以及document.location.href无法工作的原因

Javascript 试图了解表单如何工作以及document.location.href无法工作的原因,javascript,html,forms,Javascript,Html,Forms,scripts.js function homepageInput() { var x = document.getElementById('homeForm').elements["whatdoyouwant"].value; if(x.localeCompare("working") == 0){ document.location.href = 'new.html'; } } index.html <!DOCTYPE html> <

scripts.js

function homepageInput() {
    var x = document.getElementById('homeForm').elements["whatdoyouwant"].value;
    if(x.localeCompare("working") == 0){
        document.location.href = 'new.html';
    }
}
index.html

<!DOCTYPE html>
<html>
<head>

    <link rel="stylesheet" type="text/css" href="style.css">
    <script type="text/javascript" src="scripts.js"></script>
</head>
<body>
    <div class= "boxContainer">
        <div id = "box">
            <form id="homeForm" onsubmit="homepageInput()">
                <input type="password" name="whatdoyouwant" id="whatdoyouwant" placeholder="WTF..."/>
                <input id="submit" type="submit"/>
            </form>
        </div>
    </div>
</body>
</html>


当我在密码字段中输入“working”时,它不会加载位于同一目录中的new.html页面。有什么想法吗?当我在同一if语句中使用警报时,它的加载是否正常

提交表单时触发函数

您不能阻止窗体的默认行为

因此,您可以设置
location.href
,然后运行表单提交,并将表单提交到当前URL


解决这个问题的快速方法是
从onsubmit函数返回false


现代代码将绑定事件处理程序并使用

提交表单时触发函数

您不能阻止窗体的默认行为

因此,您可以设置
location.href
,然后运行表单提交,并将表单提交到当前URL


解决这个问题的快速方法是
从onsubmit函数返回false


现代代码将绑定事件处理程序并使用

谢谢:)新的,我把return false放在函数末尾,什么也没做:/。这种默认的表单行为是什么?我在哪里可以了解它。我发誓W3学校是Cancert默认表单行为是“将表单中的所有用户输入,放入字符串,根据将该信息发送到操作中指定的URL(如果未指定,则加载当前URL)来加载新页面”。你确定你是从onsubmit函数的末尾返回false吗?而不是从homepageInput函数返回false吗?哦,我没想到html onsubmit被归类为函数:/哈哈,太傻了,第一次在编解码器之后玩html、css javascript。谢谢你的帮助,现在开始工作了:3谢谢:)对于这一点,我在f my function未执行任何操作:/。此默认表单行为是什么,或者我可以在哪里阅读它。我发誓W3学校已被取消默认表单行为是“将表单中的所有用户输入,放入字符串,根据发送到操作中指定的URL的信息加载新页面(如果未指定,则加载当前URL)。是否确实从onsubmit函数的结尾返回false?而不是从你的主页输入功能?哦,我没想到提交的html被归类为一个功能:/哈哈,太傻了,第一次在编解码器后玩html,css javascript。谢谢你的帮助,现在正在工作:3