admin管理员组文章数量:1403221
I have a webapp with multiple forms and inside these forms multiple custom made ponents: input, textarea, selectbox, datepicker, radiobutton, checkbox, ... .
I found out that the submit function is fired when pressing the enter key inside a child ponent of a form tag. Something I don't want. I want to be able to use the enter key for other things like conforming a selection in a dropdown.
Form example
<template>
<form @submit.prevent="handleLogin">
<fieldset :disabled="isSubmitting" class="space-y-6">
<Input :label="$tc('email', 1)" type="email" id="email" v-model="user.email" :error="errors.email" />
<Input :label="$tc('password', 1)" type="password" id="password" v-model="user.password" :error="errors.password" />
<Select :label="$tc('role', 1)" id="role" :options="roles" displayProperty="display_name" valueProperty="id" v-model="user.role" :error="errors.role" />
<SubmitButton :label="$tc('register', 1)" :submittingLabel="$tc('register_loader', 1)" :isSubmitting="isSubmitting" />
</fieldset>
</form>
</template>
SubmitButton.vue
<button type="submit">{{ isSubmitting ? submittingLabel : label }}</button>
Therefore I'm looking for a way to prevent the default behaviour. Adding a keydown function and checking if the enter key is being pressed inside all the custom ponents followed by an event.preventDefault()
didn't do the trick.
A working solution should be to change the type of the button from 'submit' to 'button' and use an @click but that doesn't sound like semantic html.
Any other suggestions?
I have a webapp with multiple forms and inside these forms multiple custom made ponents: input, textarea, selectbox, datepicker, radiobutton, checkbox, ... .
I found out that the submit function is fired when pressing the enter key inside a child ponent of a form tag. Something I don't want. I want to be able to use the enter key for other things like conforming a selection in a dropdown.
Form example
<template>
<form @submit.prevent="handleLogin">
<fieldset :disabled="isSubmitting" class="space-y-6">
<Input :label="$tc('email', 1)" type="email" id="email" v-model="user.email" :error="errors.email" />
<Input :label="$tc('password', 1)" type="password" id="password" v-model="user.password" :error="errors.password" />
<Select :label="$tc('role', 1)" id="role" :options="roles" displayProperty="display_name" valueProperty="id" v-model="user.role" :error="errors.role" />
<SubmitButton :label="$tc('register', 1)" :submittingLabel="$tc('register_loader', 1)" :isSubmitting="isSubmitting" />
</fieldset>
</form>
</template>
SubmitButton.vue
<button type="submit">{{ isSubmitting ? submittingLabel : label }}</button>
Therefore I'm looking for a way to prevent the default behaviour. Adding a keydown function and checking if the enter key is being pressed inside all the custom ponents followed by an event.preventDefault()
didn't do the trick.
A working solution should be to change the type of the button from 'submit' to 'button' and use an @click but that doesn't sound like semantic html.
Any other suggestions?
Share Improve this question asked Nov 29, 2021 at 8:58 ThoreThore 1,8682 gold badges31 silver badges65 bronze badges1 Answer
Reset to default 6You can prevent submission on enter key down just annotating the top-level form.
<form @submit.prevent="handleLogin" @keydown.enter="$event.preventDefault()">
You can see it in action
本文标签: javascriptVue Prevent form submit when pressing enter inside formStack Overflow
版权声明:本文标题:javascript - Vue Prevent form submit when pressing enter inside form - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744375673a2603247.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论