如何在表单验证结束后跳转到另一个创建的页面?

huangapple go评论86阅读模式
英文:

How to move to another created page after the form validation is over?

问题

我已经创建了一个HTML表单,并在文件script.js中编写了验证逻辑。
在成功验证表单并满足条件后,它应该将用户带到下一个页面。

但实际情况并非如此。

所有这三个文件都在同一个文件夹中。

文件:Script.js

var username = document.getElementById("name").value;
var pass = document.getElementById("pass").value;

function submit() {
  if (username.length > 0 && pass.length > 0) {
    if (username === "Abi" && pass.length == 5) {
      window.open("file:///C:/Users/abiji/Desktop/RapidAPI/index1.html");
      return true;
    }
  }
  return false;
}

文件:index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>RECIPE</title>
    <link href="index.css" rel="stylesheet" />
  </head>

  <body>
    <div class="container">
      <form name="login" onsubmit="return submit()" method="get">
        <div>
          <label for="name">Name</label>
          <input type="text" id="name" required />
        </div>

        <div>
          <label for="pass">password</label>
          <input type="password" id="pass" required />
        </div>

        <button type="submit" id="but">Login</button>
      </form>
    </div>
  </body>
  <script src="script.js"></script>
</html>

文件:index1.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>First Page</title>
  </head>
  <body>
    <h1>Welcome Beautiful!</h1>
  </body>
</html>

以上是您提供的代码的翻译部分。

英文:

I have created a HTML form and i wrote the validation logic in the file script.js.
After the form is successfully validated and the condition is met, it should take the user to the next page.

But it is not happening as expected.

All these three files are in the same folder

File: Script.js

var username = document.getElementById(&quot;name&quot;).value;
var pass = document.getElementById(&quot;pass&quot;).value;

function submit() {
  if (username.length &gt; 0 &amp;&amp; pass.length &gt; 0) {
    if (username === &quot;Abi&quot; &amp;&amp; pass.length == 5) {
      window.open(&quot;file:///C:/Users/abiji/Desktop/RapidAPI/index1.html&quot;);
      return true;
    }
  }
  return false;
}

File: index.html

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
  &lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot; /&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot; /&gt;
    &lt;title&gt;RECIPE&lt;/title&gt;
    &lt;link href=&quot;index.css&quot; rel=&quot;stylesheet&quot; /&gt;
  &lt;/head&gt;

  &lt;body&gt;
    &lt;div class=&quot;container&quot;&gt;
      &lt;form name=&quot;login&quot; onsubmit=&quot;return submit()&quot; method=&quot;get&quot;&gt;
        &lt;div&gt;
          &lt;label for=&quot;name&quot;&gt;Name&lt;/label&gt;
          &lt;input type=&quot;text&quot; id=&quot;name&quot; required /&gt;
        &lt;/div&gt;

        &lt;div&gt;
          &lt;label for=&quot;pass&quot;&gt;password&lt;/label&gt;
          &lt;input type=&quot;password&quot; id=&quot;pass&quot; required /&gt;
        &lt;/div&gt;

        &lt;button type=&quot;submit&quot; id=&quot;but&quot;&gt;Login&lt;/button&gt;
      &lt;/form&gt;
    &lt;/div&gt;
  &lt;/body&gt;
  &lt;script src=&quot;script.js&quot;&gt;&lt;/script&gt;
&lt;/html&gt;

File: index1.html

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
  &lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot; /&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot; /&gt;
    &lt;title&gt;First Page&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;h1&gt;Welcome Beautiful!&lt;/h1&gt;
  &lt;/body&gt;
&lt;/html&gt;

答案1

得分: 3

将以下代码移到 submit 函数中。你之前的做法是在脚本加载时进行评估,而不是在提交表单时进行评估。因此,无论你输入什么,长度始终为0。

顺便提一下:最好使用 === 来检查相等性,而不是 ==

var username = document.getElementById("name").value;
var pass = document.getElementById("pass").value;
英文:

Move

var username = document.getElementById(&quot;name&quot;).value;
var pass = document.getElementById(&quot;pass&quot;).value;

to the submit function. The way you did that it is evaluated when the script is loaded and not when you submit the form. Therefore the length remains 0 no matter what you type in.

Sidenote: it is good practice to use === to check equality instead of ==.

答案2

得分: 1

你应该使用 window.location.href 而不是 window.open,因为它更可靠

尝试这样做:

window.location.href = "file:///C:/Users/abiji/Desktop/RapidAPI/index1.html";

而不是:

window.open("file:///C:/Users/abiji/Desktop/RapidAPI/index1.html");
英文:

You should use window.location.href instead of window.open. because it is more reliable.

Try this

window.location.href = &quot;file:///C:/Users/abiji/Desktop/RapidAPI/index1.html&quot;;

instead of

window.open(&quot;file:///C:/Users/abiji/Desktop/RapidAPI/index1.html&quot;);

答案3

得分: 1

首先,有一个内置的提交函数,它在表单提交后触发。你将你的函数命名为"submit",这导致了重复和无法调用。我将其命名为"submitEvent",现在它可以正常工作。

其次,如果HTML文件位于同一目录中,你可以简单地像这样调用它们:"./index1.html"。不要提供完整路径,这不是一个好的做法。

第三,你在作用域之外创建了"username"和"pass"参数。它们将在页面加载后创建,它们将是空白的,并且每次提交时都不会改变。请在你将创建的函数内部创建这些参数。在我的情况下,它是"submitEvent",我在其范围内创建这些参数。

当你使用"onsubmit"事件用于HTML表单标签时,你只需使用内置的提交函数,它返回一个布尔值。在你的示例中,你根据条件返回true或false。用户名"Abi"和长度为5的任何密码都可以通过你的条件,并成功重定向。

function submitEvent() {
  var username = document.getElementById("name").value;
  var pass = document.getElementById("pass").value;
  if (username.length > 0 && pass.length > 0) {
    if (username === "Abi" && pass.length == 5) {
      window.open("./index1.html");
      return true;
    }
  }
  return false;
}
英文:

First things first, there is built-in submit function, which triggers after form submitting. You named your function as "submit", which results in duplicate and uncallable. I named it as "submitEvent", and it works now.

Secondly, If html files are in the same directory, you can simply call them like "./index1.html". Don't give the full path, it is not a good practice.

Thirdly, you created "username" and "pass" parameters out of scope. They will be created after page load, they will be empty spaces and everytime you submit, nothing will change. Create these parameters inside the function that you will create. In my case, it is submitEvent and I create these parameters in its scope.

When you use "onsubmit" event for html form tag, you simply use the built-in submit function, which returns a boolean value. In your example, you return true or false based on your conditions. Username "Abi" and any password with the length of 5 can pass your conditions and successfully redirects.

function submitEvent() {
  var username = document.getElementById(&quot;name&quot;).value;
  var pass = document.getElementById(&quot;pass&quot;).value;
  if (username.length &gt; 0 &amp;&amp; pass.length &gt; 0) {
    if (username === &quot;Abi&quot; &amp;&amp; pass.length == 5) {
      window.open(&quot;./index1.html&quot;);
      return true;
    }
  }
  return false;
}

答案4

得分: 0

1- 将&lt;script src=&quot;script.js&quot;&gt;&lt;/script&gt;标签移动到HTML主体的末尾,在闭合的&lt;/body&gt;标签之前。

2- 更新你的script.js中的提交函数如下:

function submit(event) {
  event.preventDefault();

  var username = document.getElementById("name").value;
  var pass = document.getElementById("pass").value;

  if (username === "Abi" && pass.length === 5) {
    window.location.href = "index1.html";
    return true;
  }

  return false;
}

这些更改确保了表单验证的正确执行,并且只有在满足条件时才会将页面重定向到index1.html

英文:

1- Move the &lt;script src=&quot;script.js&quot;&gt;&lt;/script&gt; tag to the end of the HTML body, just before the closing </body> tag.

2- Update your submit function in script.js as follows:

function submit(event) {
  event.preventDefault();

  var username = document.getElementById(&quot;name&quot;).value;
  var pass = document.getElementById(&quot;pass&quot;).value;

  if (username === &quot;Abi&quot; &amp;&amp; pass.length === 5) {
    window.location.href = &quot;index1.html&quot;;
    return true;
  }

  return false;
}

These changes ensure that the form validation is properly performed and the page is redirected to index1.html only if the conditions are met.

huangapple
  • 本文由 发表于 2023年6月15日 19:14:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76481891.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定