简体   繁体   English

对多个元素使用相同的 v-model

[英]Using same v-model to multiple elements

I have one dropdown which is set v-model="compose.Recipient".我有一个设置为 v-model="compose.Recipient" 的下拉菜单。

According to the "compose.Recipient", i need to show-up another drop-down as following根据“compose.Recipient”,我需要显示另一个下拉列表,如下所示

<div class="form-group" v-if="compose.Recipient==2" title="<?=$this->lang->line('SELECTCLASS')?>">
      <select v-model="compose.RecipientID" >
           <option v-for="value in a" v-bind:value="value"><?=$this->lang->line('CLASS')?> {{value}}</option>
      </select>
</div>
<div class="form-group" v-else-if="compose.Recipient==3" title="<?=$this->lang->line('SELECTGRADE')?>">
      <select v-model="compose.RecipientID" >
           <option v-for="value in b" v-bind:value="value"><?=$this->lang->line('GRADE')?> {{value}}</option>
      </select>
</div>
<div class="form-group" v-else-if="compose.Recipient==4" title="<?=$this->lang->line('SELECTBUS')?>">
      <select v-model="compose.RecipientID" >
           <option v-for="value in c" v-bind:value="value"><?=$this->lang->line('BUS')?> {{value}}</option>
      </select>
</div>

It's not working because of v-if, if i use v-show it's working.由于 v-if,它不工作,如果我使用 v-show 它工作。

I'm using same v-model="compose.RecipientID" to all dropdowns therefore i could not be able to use v-show instead of v-if.我对所有下拉菜单都使用相同v-model="compose.RecipientID" ,因此我无法使用 v-show 代替 v-if。

How to solve it?如何解决?

Advanced Thanks.进阶谢谢。

Instead of using three different select elements, create a computed that represents the source of the select .不要使用三个不同的select元素,而是创建一个表示select源的计算 That computed function will look at compose.Recipient and return either a , b , or c .该计算函数将查看compose.Recipient并返回abc Use that computed as the source for the v-for (in a single select ).使用计算的作为v-for的源(在单个select中)。

This way you're only binding a single item to compose.RecipientID and you don't have to worry about showing/hiding different select elements.这样,您只需将单个项目绑定到compose.RecipientID并且您不必担心显示/隐藏不同的select元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM