Home > Web Front-end > Vue.js > body text

How to use the v-on directive to handle form input events in Vue

WBOY
Release: 2023-09-15 09:12:29
Original
667 people have browsed it

How to use the v-on directive to handle form input events in Vue

How to use the v-on directive to handle form input events in Vue

Vue is a popular JavaScript framework for building user interfaces. In Vue, we can use the v-on directive to monitor form input events, such as button clicks, keyboard input, etc. This article will introduce how to use the v-on directive to handle form input events in Vue, and provide specific code examples.

  1. Listening to button click events

First, we will discuss how to use the v-on directive to listen for button click events. Let's say we have a button that triggers a method when clicked.

HTML template code is as follows:

<div id="app">
  <button v-on:click="handleClick">点击我</button>
</div>
Copy after login

Vue instance code is as follows:

new Vue({
  el: '#app',
  methods: {
    handleClick: function() {
      alert('按钮被点击了!');
    }
  }
});
Copy after login

In the above code, we use the v-on directive to bind the click event of the button. Followed by the name of the method we want to trigger.

  1. Listen to text box input events

Next, we will introduce how to use the v-on directive to monitor text box input events. Suppose we have a text box and when the user types in the text box, a method is triggered.

HTML template code is as follows:

<div id="app">
  <input v-on:input="handleChange" type="text" placeholder="请输入内容">
</div>
Copy after login

Vue instance code is as follows:

new Vue({
  el: '#app',
  methods: {
    handleChange: function(event) {
      console.log(event.target.value);
    }
  }
});
Copy after login

In the above code, we use the v-on directive to bind the input event of the text box, This is followed by the name of the method we want to trigger. And, in the method we can get the value entered by the user through event.target.value.

  1. Listening to keyboard events

Finally, we will introduce how to use the v-on command to listen for keyboard events. Suppose we have an input box, and when the user presses the Enter key, a method will be triggered.

HTML template code is as follows:

<div id="app">
  <input v-on:keyup.enter="handleEnter" type="text" placeholder="按下回车键">
</div>
Copy after login

Vue instance code is as follows:

new Vue({
  el: '#app',
  methods: {
    handleEnter: function() {
      alert('回车键被按下了!');
    }
  }
});
Copy after login

In the above code, we use the v-on directive to bind the keyup event of the keyboard, and The .enter modifier specifies that the monitored key value is the Enter key (key code is 13). This is followed by the name of the method to be triggered.

Summary

Through the above examples, we learned how to use the v-on directive to handle form input events in Vue. We can use v-on:click to listen for button click events, v-on:input to listen for text box input events, and v-on:keyup.enter to listen for keyboard enter events. The v-on directive can be used to easily handle form input events.

Note that in Vue, v-on can be abbreviated as @, for example, @click represents v-on:click, @input represents v-on:input, and @keyup.enter represents v-on:keyup. enter. We can choose which writing method to use based on personal preference.

I hope this article can help you use the v-on instruction to handle form input events in Vue. If you have any questions, please leave a message to discuss.

The above is the detailed content of How to use the v-on directive to handle form input events in Vue. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!