使用 javascript 转到另一个页面 html

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18158191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 12:01:21  来源:igfitidea点击:

go to another page html with javascript

javascripthtml

提问by neustre

I have two files. One is login.htmlwhich is a simple html5 file with a form.

我有两个文件。一个是login.html带有表单的简单 html5 文件。

HTML

HTML

<form method="post" action="" name="form1">entre the pasword:
    <input type="password" name="code" placeholder="code" maxlength="6">
    <p class="submit">
        <input type="submit" name="commit" value="send" onclick="verif(document.form1.code)">
    </p>
</form>

Second is my javascript file with the below code:

其次是我的 javascript 文件,其中包含以下代码:

function verif(inputtxt) {

    var pwd = "123456";
    if (inputtxt.value.match(pwd)) {
        window.location.href = 'Test.html';
    } else {
        alert('Code erron1 ! ')
        return false;
    }

}

Now my problem is that when I enter my password, if it is wrongthe alert message indicating an error should appear (it appears and I don't have a problem with that) and if it is correct, I should get redirected to the next page. The second part doesn't work for me.

现在我的问题是,当我输入密码时,如果输入错误,则应该出现指示错误的警报消息(它出现并且我没有问题),如果正确,我应该被重定向到下一个页。第二部分对我不起作用。

Please help, I'm stuck with that for two days now..

请帮忙,我已经坚持了两天了..

回答by Harry

Since your button is a submit button, I think it is submitting the form after the JS is done and this could be the reason why you don't get redirected to Test.html (as form actionattribute doesn't have any value.) Try the below code for the HTML form and check if this solves the issue.

由于您的按钮是一个提交按钮,我认为它是在 JS 完成后提交表单,这可能是您没有被重定向到 Test.html 的原因(因为表单action属性没有任何值。)尝试HTML 表单的以下代码并检查这是否解决了问题。

<form method="post" action="" name="form1" onsubmit="verif(document.form1.code);return false;">entre the pasword:
    <input type="password" name="code" placeholder="code" maxlength="6">
    <p class="submit">
        <input type="submit" name="commit" value="send">
    </p>
</form>

The return false;in the onsubmitattribute prevents the form's default submit action. The verif(document.form1.code)will be executed whenever the form is submitted (that is the submit button is clicked).

return false;onsubmit属性防止表单的默认提交操作。该verif(document.form1.code)每当表单提交将被执行(即按钮被点击提交)。