[英]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应用程序结构时,您应该始终考虑您的数据结构是如何构建的:
If you have this dataflow in your head, then you can design the components:如果您脑子里有这个数据流,那么您可以设计组件:
this.$emit()
)this.$emit()
)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.