Issues with window.location.assign



我正在尝试创建一个表单来检查输入,查看它是否正确,如果是,然后加载一个新网页,但我的代码不起作用,即使输入正确,也不会加载新页面。我正在使用window.location.assign来尝试实现这一目标。

代码如下:

    <!DOCTYPE html>
    <html>
    <head>
        <title> INDEX </title>
        <meta charset="utf-8">
        <link type="text/css" rel="stylesheet" href="style.css">
        <script>
    function validateForm() {
        var x = document.forms["myForm"]["fname"].value;
        if (x == null || x == "this") {
            window.location.assign("http://www.google.com");  
        }   else {
                alert("Try again");
                return false;
            }
        }
    </script>
    </head>
    <body>
    <div id="center">
        <form name="myForm" action="demo_form.asp"
        onsubmit="return validateForm()" method="post">
        <p> type "this" </p> 
        <input type="text" name="fname">
        <input type="submit" value="Submit">
        </form>
    </div>
    </body>
    </html>

window.location.assign 没有问题,你的代码有问题,如果你真的想要 widow.location.assign 工作试试这个-

<div id="center">
    <form name="myForm" 
    onsubmit="return validateForm()">
    <p> type "this" </p> 
    <input type="text" name="fname">
    <input type="button" value="Submit" onclick="validateForm()">
    </form>
</div>