简体   繁体   English

我如何制作一个使用特定输入重定向到特定 html 页面的 js 代码?

[英]How do i make a js code that redirects to a certain html page with a certain input?

let me explain this better, i would like to know how it's possible to create a js code that checks if an html input is correct and in case it is it redirects you to another page, here is what i tried based on what i managed to find out.让我更好地解释一下,我想知道如何创建一个 js 代码来检查 html 输入是否正确,如果它会将您重定向到另一个页面,这是我根据我设法做到的尝试找出。

html part: html部分:

<form name="access" onsubmit="return validate()">
  <input
    type="text"
    id="inputbox"
    value="Password"
    pattern="idkwhatishoouldwriteinhere"
  />
  <input type="submit" value="Submit" />
</form>

js part: js部分:

function validate() {
  if (document.access.Password.value != "idkwhatishoouldwriteinhere") {
    alert("Wrong password");
    document.access.Password.focus();
    return false;
  } else {
    window.open("index.html");
  }
}

in case you are wondering why i put the "answer" in the patter is because this is supposed to be a little easter egg and i feel like looking directly at the js is meaningless becuase it contains the link you should be redirected to.如果你想知道为什么我把“答案”放在模式中是因为这应该是一个小复活节彩蛋,我觉得直接查看 js 是没有意义的,因为它包含你应该被重定向到的链接。 enter code here在这里输入代码

You need to give your input the name Password , otherwise document.access.Password is undefined.您需要输入名称Password ,否则document.access.Password未定义。

 function validate() { if (document.access.Password.value != "idkwhatishoouldwriteinhere") { alert("Wrong password"); document.access.Password.focus(); return false; } else { window.open("index.html") } }
 <form name="access" onsubmit="return validate()"> <input type="text" id="inputbox" value="Password" name="Password" /> <input type="submit" value="Submit" /> </form> <!-- password is "idkwhatishoouldwriteinhere" -->

You want this.你要这个。

You had some issues with the id of the field and name etc您在字段 ID 和名称等方面遇到了一些问题

I also changed your inline code to eventListener which is the recommended method我还将您的内联代码更改为 eventListener,这是推荐的方法

Password is fred密码是fred

 window.addEventListener("load", function() { document.getElementById("access").addEventListener("submit", function(e) { const inputbox = document.getElementById("inputbox"); if (inputbox.value != "fred") { alert("Wrong password"); inputbox.focus(); e.preventDefault(); // cancel submit } else location.replace("index.html") }); })
 <form id="access"> <input type="password" id="inputbox" value="" placeholder="Password" /> <input type="submit" value="Submit" /> </form>

If you want to keep your code close to what you already have, I would adjust it like this.如果你想让你的代码接近你已经拥有的代码,我会像这样调整它。 I would suggest storing your class names and ids as variables and then accessing them from the variable.我建议将您的类名和 id 存储为变量,然后从变量中访问它们。 Also there is no need to return false in your if.也没有必要在你的 if 中返回 false。 There are other good solutions on here but this one will keep your code pretty close.这里还有其他很好的解决方案,但这个解决方案将使您的代码非常接近。 This will also ensure that you don't end up with a null value when accessing the value in your password field.这也将确保您在访问密码字段中的值时不会得到空值。

 const passwordField = document.getElementById('inputbox'); function validate() { if(passwordField.value != "idkwhatishoouldwriteinhere") { alert( "Wrong password" ); passwordField.focus() ; } else { window.open("index.html") } }
 <form name="access" onsubmit="validate()" href="javascript:void(0)"> <input type="text" id="inputbox" value="Password" /> <input type="submit" value="Submit" /> </form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我希望我的 html 页面在我提交某个输入时重定向到另一个 html 页面我该怎么做 - I want my html page to redirect to another html page when I submit a certain input how can i do that 如果对象到达页面上的某个点,如何使对象停止? - How do I make an object stop if it reaches a certain point on a page? 如何使CSS代码运行一定的时间? - How do I make css code run for a certain amount of time? 如何制作某些命令 arguments? - How do I make certain command arguments? 如何在页面上抓取某些文本? - How do I grab certain text on a page? 如果选中某个复选框,我该如何做到在文本框中需要输入? - How do I make it so that an input is required in a textbox if a certain checkbox is selected? 如何使用Javascript检查HTML输入名称是否具有特定值? - How do I check if an HTML input name has a certain value with Javascript? 如何根据页面中其他位置的文本停止某些输入值? - How do I stop certain values of input, based on text elsewhere in the page? 如何根据下拉选择取消某些HTML输入字段? - how do I suppress certain HTML input fields based on drop down selection? 在html中选择某些选项时如何使某些输入字段出现? - How to make certain input fields appear when selected certain option in html?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM