[英]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.