简体   繁体   English

我应该为密码表单使用什么输入类型?

[英]What input type should I use for password form?

I'm trying to make a password form for my portfolio website.我正在尝试为我的投资组合网站制作密码表格。 I'm having a problem with the input type.我的输入类型有问题。

When I have the input type as 'submit', typing password and hitting 'return' key will trigger the checkPswd() function.当我将输入类型设置为“提交”时,输入密码并点击“返回”键将触发 checkPswd() function。 However, it does not link my website to 'index2.html'.但是,它不会将我的网站链接到“index2.html”。

When I have the input type as 'button', the problem is the other way around.当我将输入类型设置为“按钮”时,问题就相反了。 Now hitting 'return' key won't trigger the checkPswd().现在点击“return”键不会触发 checkPswd()。 However, it does link to 'index2.html' when I click on the submitubtton with mouse key.但是,当我用鼠标键单击 submitubtton 时,它确实链接到“index2.html”。

I've also tried to trigger the checkPswd with (event.keyCode === 13), but it diddn't work.我也尝试使用 (event.keyCode === 13) 触发 checkPswd,但它没有用。 Does anybody have a solution?有人有解决方案吗?

    <div class="overlay_top">
          <div class="row justify-content-center" style="padding-top: 10%;">
                <form name="login">
                  <input type="password" id="pswd" class="form-control mx-sm-3" aria-describedby="passwordHelpInline" placeholder="Password" style="border-radius: 0rem; width:240px;">
                  <input type="submit" id="submitbutton" value="Submit" onclick=checkPswd() class="btn btn-white" style="border-radius: 0rem; width:240px; font-weight: 700;">
                </form>
          </div>
    </div>


    <script>
      function checkPswd() {
          var confirmPassword = "admin";
          var password = document.getElementById("pswd").value;
          if (password == confirmPassword) {
              window.location.href="index2.html";
          }
          else{
              alert("Passwords do not match.");
          }
      }
    </script>

Forms naturally will want to send a request to the server which triggers a reload of the page. Forms 自然会想向服务器发送一个请求来触发页面的重新加载。 what you can do is to use and eventListener (so that you can prevent that behavior) instead of an having the handler in-line just the way you have it now.你可以做的是使用和eventListener (这样你就可以防止这种行为),而不是像你现在拥有的那样让处理程序内联。 This is what you can try:这是您可以尝试的:

<div class="overlay_top">
<div class="row justify-content-center" style="padding-top: 10%;">
  <form name="login">
    <input type="password" id="pswd" class="form-control mx-sm-3" aria-describedby="passwordHelpInline"
      placeholder="Password" style="border-radius: 0rem; width:240px;">
    <input type="submit" id="submitbutton" value="Submit" class="btn btn-white"
      style="border-radius: 0rem; width:240px; font-weight: 700;">
  </form>
</div>
const submitBtnElem = document.querySelector('#submitbutton');

submitBtnElem.addEventListener('click', (e) => {
  e.preventDefault();
  checkPswd();
});

function checkPswd() {
  var confirmPassword = "admin";
  var password = document.getElementById("pswd").value;
  if (password == confirmPassword) {
    window.location.href = "index2.html";
  }
  else {
    alert("Passwords do not match.");
  }
}

happy coding.快乐编码。 #cheers #干杯

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

相关问题 反应:当我在表单中使用 input type="file' 时,我应该在 propTypes 中写什么? - REACT: What I should write in propTypes when in form I use input type="file'? 我应该为 Reactstrap 输入参考使用什么 typescript 类型? - What typescript type should I use for Reactstrap Input Ref? 我应该使用什么类型的加密 - What type of encryption should i use 对于结合了文本和数字的代码,我应该使用什么输入类型? 如何在 TypeScript 中声明以避免错误? - What input type should I use for code which combines text and numbers? How to declare in TypeScript to avoid errors? 我应该使用什么流星事件处理程序来提交onchange表单 - What Meteor event handler should I use for onchange form submits 我应该使用什么按钮事件来确认 html 表单? - What button event should I use to confirm a html form? 我应该为模态表单使用什么事件处理程序来发送 email - What event handler should i use for modal form to send an email 我应该使用什么事件在jQuery中异步提交表单? - What event should I use to submit a form asynchronously in jQuery? 这种类型的下拉菜单应该使用什么jquery事件 - what jquery event should i use for this type of dropdown menu 我应该为未知的字符串枚举参数使用什么类型? - What type should I use for an unknown string enum argument?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM