Vue.js在@change上获得选择选项

hph*_*php 50 html javascript asp.net-mvc vue.js

首先,我想说的是Vue的新手,这是我第一个使用Vue的项目.我有组合框,我想根据所选的组合框做一些不同的事情.我使用单独的vue.html和typescript文件.这是我的代码.

<select name="LeaveType" @change="onChange()" class="form-control">
     <option value="1">Annual Leave/ Off-Day</option>
     <option value="2">On Demand Leave</option>
</select>
Run Code Online (Sandbox Code Playgroud)

这是我的ts文件

onChange(value) {
    console.log(value);
}
Run Code Online (Sandbox Code Playgroud)

如何在我的打字稿功能中获取选定的选项值?谢谢.

ram*_*win 95

使用v-model所选选项的值的值绑定.这是一个例子.

<select name="LeaveType" @change="onChange($event)" class="form-control" v-model="key">
   <option value="1">Annual Leave/ Off-Day</option>
   <option value="2">On Demand Leave</option>
</select>
<script>
var vm = new Vue({
    data: {
        key: ""
    },
    methods: {
        onChange(event) {
            console.log(event.target.value)
        }
    }
}
</script>
Run Code Online (Sandbox Code Playgroud)

这里可以看到更多参考.


san*_*era 21

@v-on的快捷方式选项.仅在要执行某些Vue方法时使用@.由于您没有执行Vue方法,而是调用javascript函数,您需要使用onchange属性来调用javascript函数

<select name="LeaveType" onchange="onChange(this.value)" class="form-control">
 <option value="1">Annual Leave/ Off-Day</option>
 <option value="2">On Demand Leave</option>
</select>

function onChange(value) {
  console.log(value);
}
Run Code Online (Sandbox Code Playgroud)

如果你想调用Vue方法,那就这样做 -

<select name="LeaveType" @change="onChange($event)" class="form-control">
 <option value="1">Annual Leave/ Off-Day</option>
 <option value="2">On Demand Leave</option>
</select>

new Vue({
  ...
  ...
  methods:{
    onChange:function(event){
       console.log(event.target.value);
    }
  }
})
Run Code Online (Sandbox Code Playgroud)

您可以在select元素上使用v-model数据属性来绑定值.

<select v-model="selectedValue" name="LeaveType" onchange="onChange(this.value)" class="form-control">
 <option value="1">Annual Leave/ Off-Day</option>
 <option value="2">On Demand Leave</option>
</select>

new Vue({
    data:{
        selectedValue : 1, // First option will be selected by default
    },
    ...
    ...
    methods:{
        onChange:function(event){
            console.log(this.selectedValue);
        }
    }
})
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助 :-)


Agn*_*ney 18

更改的值将在 event.target.value

const app = new Vue({
  el: "#app",
  data: function() {
    return {
      message: "Vue"
    }
  },
  methods: {
    onChange(event) {
      console.log(event.target.value);
    }
  }
})
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
<div id="app">
  <select name="LeaveType" @change="onChange" class="form-control">
   <option value="1">Annual Leave/ Off-Day</option>
   <option value="2">On Demand Leave</option>
</select>
</div>
Run Code Online (Sandbox Code Playgroud)


Mer*_*aye 12

您也可以使用v-model进行救援

<template>
    <select name="LeaveType" v-model="leaveType" @change="onChange()" class="form-control">
         <option value="1">Annual Leave/ Off-Day</option>
         <option value="2">On Demand Leave</option>
    </select>
</template>

<script>
export default {
    data() {
        return {
            leaveType: '',
        }
    },

    methods: {
        onChange() {
            console.log('The new value is: ', this.leaveType)
        }
    }
}
</script>
Run Code Online (Sandbox Code Playgroud)


Rxh*_*com 6

onChangeMethod --> 您选择的方法,它可以是与您的程序相关的任何方法。

<template>
<select @change="onChangeMethod($event)">                                            
  <option value="test">Test</option>
  <option value="test2">Test2</option>
</select>
</template>

<script>
 data(){
   return{
    ...
     
   }
 },
 methods:{
     onChangeMethod(event)
     {
         console.log(event.target.value);
     }
 },
 created(){
   ...
 }   
</script>                                                                          
Run Code Online (Sandbox Code Playgroud)