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

How to get the value of the drop-down box in vue

下次还敢
Release: 2024-05-02 21:42:13
Original
852 people have browsed it

In Vue, there are two ways to obtain the value of the drop-down box: 1. Use the v-model directive for two-way binding; 2. Locally listen to the change event of the drop-down box and obtain the value.

How to get the value of the drop-down box in vue

How to get the value of the drop-down box in Vue

In Vue, there are two ways to get the drop-down box Value:

1. v-model

v-model directive can bidirectionally bind the value of the drop-down box and Vue data, in the component Use it in the template:

<code class="html"><select v-model="selectedValue">
  <option value="option1">选项 1</option>
  <option value="option2">选项 2</option>
</select></code>
Copy after login

Then access the selectedValue data in JavaScript:

<code class="javascript">const selectedValue = this.selectedValue;</code>
Copy after login

2. Local event listening

You can listen to the change event on the drop-down box and get the value in the event handler:

<code class="html"><select @change="handleChange">
  <option value="option1">选项 1</option>
  <option value="option2">选项 2</option>
</select></code>
Copy after login
<code class="javascript">methods: {
  handleChange(event) {
    const selectedValue = event.target.value;
  }
}</code>
Copy after login

No matter which method is used, you can easily get the value of the drop-down box in Vue.

The above is the detailed content of How to get the value of the drop-down box in vue. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
vue
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!