嘿大家所以我试图制作一个vueapp,当用户点击选项标签时设置属性。我正在尝试使用v-on:点击但它没有用。有任何想法吗?
<div v-show="loggedin==1" class="searchBy">
<h2>Selected Display</h2>
<select v-model="selectedDisplayShapeText">
<option value="" disabled>Please Choose One</option>
<option v-for="display in displays" :value="display.shapetext" v-on:click="displayName=display.name">{{display.name}}</option>
</select>
</div>
答案 0 :(得分:1)
您可以将Vue中select选项的值绑定到复杂表达式(在本例中为对象)。
不是将所选选项绑定到显示对象的属性,而是将其绑定到显示对象本身。然后,只要您需要所选显示的某个属性,就可以从所选显示中引用它们。
这是一个例子。
console.clear()
new Vue({
el: "#app",
data:{
displays: [
{shapetext: "shape text 1", name: "display one"},
{shapetext: "shape text 2", name: "display two"},
{shapetext: "shape text 3", name: "display three"},
],
selectedDisplay: {}
}
})
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.js"></script>
<div id="app">
<h2>Selected Display</h2>
<select v-model="selectedDisplay">
<option value="" disabled>Please Choose One</option>
<option v-for="display in displays" :value="display" >{{display.name}}</option>
</select>
<hr>
Selected Display shapetext: {{selectedDisplay.shapetext}} <br>
Selected Display name: {{selectedDisplay.name}}
</div>
&#13;
答案 1 :(得分:1)
您可以在select:
的change
事件中检索该选项
new Vue({
el: "#app",
data() {
return {
displays: [{ name: "Display1", shapetext: "1" }, { name: "Display2", shapetext: "2" }, { name: "Display3", shapetext: "3" }],
selectedDisplayShapeText: '',
displayName: ''
}
},
methods: {
getDisplayName(e){
let value = e.target.value
let display = this.displays.find(d => d.shapetext == value)
this.displayName = display.name
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.min.js"></script>
<div id="app">
<h2>Selected Display</h2>
<select v-model="selectedDisplayShapeText" v-on:change="getDisplayName($event)">
<option value="" disabled>Please Choose One</option>
<option v-for="display in displays" :value="display.shapetext">{{display.name}}</option>
</select>
<p>Display selected: {{ displayName }}</p>
</div>