简体   繁体   English

@keyup.enter 仅在选项卡设置为登录按钮时有效

[英]@keyup.enter only works when tab is set to the login button

The only time my login button works is when I a direct the tab button after inputting email and password,我的登录按钮唯一有效的时间是当我在输入 email 和密码后直接使用选项卡按钮时,

I want it to work anytime I press enter and I am on the Login page.我希望它在我按下回车键并且我在登录页面上的任何时候都能正常工作。

this is a vueJS application这是一个 VueJS 应用程序

Html template Html 模板

<template>
        <div class="login-page" id="login-page">
            <div class="form" id="form">
                <form class="login-form" id="login-form">
                <div class="head-login">
                <img class="teamfu-image" src="../assets/teamfu-v.png"/><h2 class="login">Login</h2>
                </div>
                <hr class="beneathBorder" color="white">
                <div>
                    <p class="text">Email:<input v-model="input.email" class="input" type="text" placeholder='e.g. johndeer@gmail.com'/></p>
                    <p class="text">Password:<input v-model="input.password" class="input" type="password" placeholder=" " /></p>
                </div>
                <button type="button" @click="login" @keyup.enter="login" > Login</button>
                <p class="message">Not a part of the team? <router-link class="reg" :to="'/register'">Register</router-link></p>
                </form>
            </div>
        </div> 
</template>

Script脚本

<script>
import { checkUser } from "../db/storage";

export default {
    name : 'login-page',
    data() {
        return{
            input: {
                email: '',
                password: ''
            }
        }
    },
    methods : {
        login() {
            if(this.input.email != "" && this.input.password != "") {

                if(checkUser(this.input.email,  this.input.password) ) {

                    this.$emit("authenticated", true);
                    this.$router.replace('/dashboard');

                } else {

                alert("The username and / or password is incorrect")
                console.log("The username and / or password is incorrect");
            }
        } else {
            
            alert("A username and password must be present")
            console.log("A username and password must be present");
        }
        }
    }
}
</script>

If you want a page-level keyup handler, you will need to apply it as high on your tree as you can:如果你想要一个页面级的 keyup 处理程序,你需要在你的树上尽可能高地应用它:

<div class="login-page" @keyup.enter="login" id="login-page">

In this case, it would be better to use the form's submit event to invoke login() instead of using click / keyup handlers on the button.在这种情况下,最好使用表单的submit事件来调用login()而不是在按钮上使用click / keyup处理程序。 The <form> automatically emits the submit event when the user clicks the form's submit button, or when the user hits ENTER while the form is focused. <form>当用户单击表单的提交按钮时,或者当用户在表单获得焦点时按下ENTER时,会自动发出submit事件。

First, make it a submit- button , and remove the existing click / keyup handler:首先,将其设为提交button ,并移除现有的click / keyup位处理程序:

<!-- ❌
<button type="button" @click="login" @keyup.enter="login">Login</button>
-->
<button type="submit">Login</button>

Then add a submit -handler to the form:然后在表单中添加一个submit处理程序:

<form @submit.prevent="login">

demo 演示

I deleted this:我删除了这个:

❌
<button type="button" @click="login" @keyup.enter="login" > Login</button>

and used this instead and it worked并改用它,它起作用了

<input type="submit" value="login" @keyup.enter="login()">

thank for your responses, I appreciate it感谢您的回复,我很感激

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM