简体   繁体   English

VueJS:输入动态值+ v-model

[英]VueJS: input with dynamic value + v-model

I have a problem with VueJS when setting the value of an input radio AND v-model.在设置输入单选和 v-model 的值时,我遇到了 VueJS 的问题。 I cant understand why I can not setting dynamically a value to an input and use a model to retrive what input the user selected.我不明白为什么我不能动态地为输入设置值并使用模型来检索用户选择的输入。

In code is easier to understand:在代码中更容易理解:

 export default { props: ["question", "currentQuestion"], data() { return { answer: undefined } }, computed: { isCurrent() { return this.currentQuestion && this.currentQuestion.id == this.question.id; } }, methods: { groupName(question) { return 'question_id_' + question.id }, inputType(question) { if (question.question_type_id == 2) return "checkbox"; return "radio"; } }, mounted() { console.log('Component mounted.') } }
 <template> <ul v-if="question.question_type_id != 4"> <li v-for="option in question.options" :key="option.id"> <div class="form-group"> <label> <input v-model="answer" :value="option.id" :type="inputType(question)" :name="groupName(question)" /> {{option.option}} </label> </div> </li> </ul> </template>

So, in case there are 4 options, the user would see 4 radio buttons, each one with the "option.id" as a value and, WHEN the user clicks the radio button, the model "answer" would be populated with that value.因此,如果有 4 个选项,用户将看到 4 个单选按钮,每个单选按钮都有“option.id”作为值,当用户单击单选按钮时,模型“答案”将填充该值.

Now, when I try to compile this file, I get this error message:现在,当我尝试编译此文件时,我收到以下错误消息:

  • :value="option.id" conflicts with v-model on the same element because the latter already expands to a value binding internally :value="option.id" 与同一元素上的 v-model 冲突,因为后者已经在内部扩展为值绑定

So, could anyone help me understand how do I dynamically set a value to an input AND asssociate a model to retrieve the value when the input is selected?那么,任何人都可以帮助我理解如何为输入动态设置值并关联模型以在选择输入时检索值?

By the way, VueJS documentation at this page says exactly what I am trying to do: https://v2.vuejs.org/v2/guide/forms.html顺便说一句,这个页面上的 VueJS 文档正是我想要做的事情: https ://v2.vuejs.org/v2/guide/forms.html

v-model 与 v-bind:value 在同一输入中

Any help is very appreciated.非常感谢任何帮助。

The main issue here is that you have a dynamic type on the input element, so I expect Vue is getting a little confused.这里的主要问题是输入元素上有一个动态类型,所以我预计 Vue 会有点困惑。 value is not valid in combination with v-model for a checkbox input, unless you are binding to an array. value与复选框输入的v-model组合无效,除非您绑定到数组。

You can solve that by using a v-if/v-else.你可以通过使用 v-if/v-else 来解决这个问题。

<label>
  <input v-if="question.question_type_id == 2" 
         v-model="answer" 
         type="checkbox" 
         :name="groupName(question)" />
  <input v-else 
         v-model="answer" 
         :value="option.id" 
         type="radio" 
         :name="groupName(question)" />
  {{option.option}}
</label> 

Working example .工作示例

There are other issues, but this addresses the error you mention in the question.还有其他问题,但这解决了您在问题中提到的错误。 For one, it doesn't make much sense for more than one checkbox to be bound to a single v-model , unless you are binding to an array.一方面,将多个复选框绑定到单个v-model没有多大意义,除非您绑定到数组。 In that case, you would have to swap what type answer is based on whether is a radio, a checkbox with a single value or a checkbox with multiple values.在这种情况下,您必须根据是单选、具有单个值的复选框还是具有多个值的复选框来交换answer的类型。 Seems like it would get complicated.好像会变得复杂。

the proper way is to use placeholder.正确的方法是使用占位符。

<input :placeholder="option.id" v-model="answer" @input="functionToChangeValue($event)"/>

! DO NOT USE VALUE AND V-MODEL TOGETHER it is because v-model create two way biding and your code brake不要同时使用 VALUE 和 V-MODEL 这是因为 v-model 创建了双向竞价和您的代码制动

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

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