最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to handle an "enter" in a q-inputpreventDefault on submit? - Stack Overflow

programmeradmin5浏览0评论

I have a text input field, something like:

        <q-input
          @blur="checkTextAnswer"
          @keyup.enter="submit"
          @keydown="checkEnterKey"
          v-model.trim="textInput"

When the user hits enter I want to treat it like a submit, ie to handle the input and not add an extra newline in the text.

It's a bit like preventDefault from JQuery days. I did find this: .html but seems for more general DOM events

I also tried just modifying the string (str.replace the newline) but even that hack has an ugly delay.

I have a text input field, something like:

        <q-input
          @blur="checkTextAnswer"
          @keyup.enter="submit"
          @keydown="checkEnterKey"
          v-model.trim="textInput"

When the user hits enter I want to treat it like a submit, ie to handle the input and not add an extra newline in the text.

It's a bit like preventDefault from JQuery days. I did find this: https://quasar-framework.org/components/other-utils.html but seems for more general DOM events

I also tried just modifying the string (str.replace the newline) but even that hack has an ugly delay.

Share Improve this question asked Dec 1, 2018 at 14:21 dcsandcsan 12.3k17 gold badges90 silver badges135 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 20

You need to use the vue event modifier ".prevent" in your event. It also needs to be a @keydown event since the "add newline" event is called with @keydown events in inputs of type "textarea".

The solution would be:

     <q-input
      type="textarea"
      @keydown.enter.prevent="submit"
      v-model.trim="textInput"

EDIT:

The 'submit' is a method that you have to define. Here's an example I made in codepen:

Codepen example

If you instead want to submit a form when pressing enter you can just use javascript for this.

this.$refs[refKeyYouGaveToYourForm].submit()
发布评论

评论列表(0)

  1. 暂无评论