简体   繁体   English

将输入文本添加到 vue js 中的复选框值

[英]Add input text to value of checkbox in vue js

I want to get the value of the checkbox that is checked and the input that was given by the user for that specific checkbox.我想获取选中的复选框的值以及用户为该特定复选框提供的输入。 I can get the value of checked checkbox the problem is getting the value of input text.我可以获取选中复选框的值问题是获取输入文本的值。

Issue One :It works but is there a better way of doing it ??????问题一:它有效,但有没有更好的方法???? Issue Two : There are other checkbox in the form when the checkcbox is clicked a second time instead of disbling all other checkbox get clicked , when clicked third time it gets disabled问题二:第二次单击复选框而不是禁用所有其他复选框时,表单中还有其他复选框被单击,第三次单击时它被禁用

Note : Noob here Please advise注意:这里的菜鸟请指教

<template>
  <div >
    <form>
      
      <input type="checkbox" v-bind:value="question" @change="getAnswer($event)" v-model="correctAnswer" />
      <label for="question">Question</label>
    </form>

    <div id="preview">
      <p>Correct Answer :{{ model.correctAnswer }}</p>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      model: {
        question: "",
        correctAnswer: "",
      },
    };
  },
  methods: {
    getAnswer: function() {
   
      if(this.model.correctAnswer==""){
         this.model.correctAnswer=this.model.question
    
      }
      else{
        this.model.correctAnswer =""
      }
     
    },
  },
};
</script>

It's not clear to me what you want, but I try to guess: you would like to build a poll template builder - am I right?我不清楚你想要什么,但我试着猜测:你想构建一个投票模板构建器 - 我是对的吗?

If yes then here's a snippet for you to use:如果是,那么这里有一个片段供您使用:

 // component for one option const QuestionOption = { props: ['idx'], template: ` <div> <input type="checkbox" v-model="isCorrect" @change="setCorrect(isCorrect)" /> <label>Option {{ idx + 1 }}:</label> <input type="text" v-model="optionText" @input="setOptionText(optionText)" /> </div> `, data() { return { isCorrect: false, optionText: '' } }, methods: { setCorrect(e) { this.$emit('set-correct', { idx: this.idx, isCorrect: e }) }, setOptionText(text) { this.$emit('set-option-text', { idx: this.idx, optionText: text }) } } } // component for the whole question const QuestionItem = { components: { QuestionOption }, template: ` <div> Question: <input type="text" v-model="question" /> <question-option v-for="(item, idx) in correctAnswers" :key="idx" :idx="idx" @set-correct="setCorrect" @set-option-text="setOptionTextAtIdx" /> </div> `, data() { return { question: '', correctAnswers: [false, false, false, false], optionTexts: [] } }, methods: { setCorrect(e) { this.correctAnswers[e.idx] = e.isCorrect this.setQuestionData(this.question, this.correctAnswers, this.optionTexts) }, setOptionTextAtIdx({ idx, optionText }) { this.optionTexts[idx] = optionText this.setQuestionData(this.question, this.correctAnswers, this.optionTexts) }, setQuestionData(question, correctAnswers, optionTexts) { this.$emit('set-question-data', { question, correctAnswers, optionTexts }) } }, watch: { question: function(val) { this.setQuestionData(val, this.correctAnswers, this.optionTexts) } } } // component for previewing the question & correct answers const Preview = { props: ['question'], computed: { correctAnswerList() { return this.question.correctAnswers.map((e, idx) => { return { isCorrect: e, text: `Option ${ idx + 1 }` } }).filter(({ isCorrect }) => isCorrect).map(({ text }) => text).join(', ') } }, template: ` <div> Question: {{ question.question }}<br /> <div v-for="(option, idx) in question.correctAnswers" :key="idx + '-' + Date.now()" > Option {{ idx + 1 }}: {{ question.optionTexts[idx] }} </div> <div> Correct Answers: {{ correctAnswerList }} </div> </div> ` } // Vue instance - aggregating the data for the question new Vue({ el: '#app', components: { QuestionItem, Preview }, data() { return { question: { question: '', correctAnswers: [], optionTexts: [] } } }, methods: { setPreview(questionData) { this.question = questionData } } })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="app"> <question-item @set-question-data="setPreview"></question-item> <preview :question="question"></preview> </div>

When you think about the Vue app structure you should always think about how your data structure builds up:当您考虑Vue应用程序结构时,您应该始终考虑您的数据结构是如何构建的:

  • you have options, that are either true or false AND have a variable text你有选项,要么是真的,要么是假的,并且有一个可变的文本
  • the question should always "know" which of its options are "correct answers" and what is the text content of the options AND also has the question (variable text)问题应该总是“知道”它的哪些选项是“正确答案”,选项的文本内容是什么,并且还有问题(可变文本)
  • the main Vue instance should always "know" all the data the question "knows", because there's another branch of component(s) that should be updated (the preview)主 Vue 实例应该始终“知道”问题“知道”的所有数据,因为还有另一个组件分支需要更新(预览)
  • the preview is just a presentational component - it gets everything from its parent (in this case the Vue instance)预览只是一个展示组件 - 它从其父级(在本例中为 Vue 实例)获取所有内容

If you have this dataflow in your head, then you can design the components:如果您脑子里有这个数据流,那么您可以设计组件:

  • one for the option (used many times) - it should hold data and should inform its parent about the changes in its data (hence the emitted events: this.$emit() )一个用于选项(多次使用) - 它应该保存数据并应该通知其父级其数据的更改(因此发出的事件: this.$emit()
  • one for the whole question - it should contain the options & the question itself and should also inform its parent if something changes in its content (emit & re-emit events)一个针对整个问题 - 它应该包含选项和问题本身,如果内容发生变化(发出和重新发出事件),还应该通知其父级
  • one for the preview that displays the question setup一个用于显示问题设置的预览
  • one Vue instance that holds the things together一个将所有东西放在一起的 Vue 实例

And voilá!瞧! This small app is designed - now you just have to code it (snippet).这个小应用程序是经过设计的 - 现在您只需对其进行编码(代码段)。

I understand that for a beginner this may seem a bit too complex for a task such as you described (or I understood), but this is more about the logic how to build something that is easily extendable.我知道对于初学者来说,这对于像您描述的(或我理解的)这样的任务来说似乎有点过于复杂,但这更多的是关于如何构建易于扩展的东西的逻辑。 For example: if you just initialize the QuestionItem with one more false in its correctAnswers data, then you have one more Option to use.例如:如果你只是在它的正确correctAnswers数据中再用一个false来初始化QuestionItem ,那么你还有一个Option可以使用。

The answer by @muka.gergely is the right way to do the entire app . @muka.gergely 的答案是完成整个应用程序的正确方法。

If only the checkbox and input binding needs to be done .如果只需要完成复选框和输入绑定。 It can be done this way可以这样做

<input type="checkbox"  v-bind:data-id="model.question" @change="getAnswer($event)"  />
      <label for="question">Question</label>
      <input type="text" required v-model.lazy="model.question" />


methods:{
    getAnswer: function(e){
     let xray = e.currentTarget.getAttribute('data-id')
console.log(xray)
      if(this.model.correctAnswer==""){
         this.model.correctAnswer=xray
    
      }
      else{
        this.model.correctAnswer =""
      }
    }
  }

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

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