【问题标题】:Vue - Multiple radio inputs, checked value?Vue - 多个无线电输入,检查值?
【发布时间】:2018-03-30 22:08:43
【问题描述】:

我在创建一个简单的投票生成器时正在学习 vue,但我遇到了无线电输入问题。

问题可以有两种类型之一 - 选择或范围(两者都是单选输入,“选择”是单选题,“范围”是单选题,有 1-5 个值,如图所示下图;稍后我将不得不修复命名...)。

问题是,当我呈现投票时,我有与问题一样多的无线电输入组,并且选择一个值不会检查无线电输入。

我通过 axios.get 获得了包含问题和可能答案的民意调查。

模板片段:

  <label>Ankieta</label>
  <div class="card mb-2" v-for="question in poll.Questions">
    <div class="card-header p-2">{{question.question}}</div>
    <div class="card-body pb-1">
      <div class="form-group" v-if="question.type === 'radio'" >
        <div v-for="answer in question.QuestionAnswers">
          <input type="radio" :name="'question'+question.id" v-bind:value="answer.answer" v-on:click.prevent="updateAnswer(question, answer)">
          <label v-bind:for="answer.id">{{answer.answer}}</label>
        </div>
      </div>
      <div v-else-if="question.type === 'range'">
        <p class="text-muted mb-4 d-flex justify-content-center">W jakim stopniu zgadzasz się ze stwierdzeniem w pytaniu?<br/>1 - Bardzo się nie zgadzam, 3 - Neutralny/a, 5 - Bardzo się zgadzam<br></p>
        <div class="form-group d-flex justify-content-center" >
          <label class="radio-inline d-flex justify-content-center w-100" v-for="answer in question.QuestionAnswers">
            <input type="radio" :name="'question'+question.id" v-bind:value="answer.answer" v-on:click.prevent="updateAnswer(question, answer)"><span class="text-muted font-weight-bold m-1">{{answer.answer}}</span>
          </label>
        </div>
      </div>
    </div>
  </div>

数据:

data() {
  return {
    submission: {
      email: undefined,
      age: undefined,
      answers: []
    },
    poll: {},
    loading: false
  }
}

更新答案:

  updateAnswer(question, answer) {
    let index = this.submission.answers.findIndex(q => q.questionId === question.id);
    let ans = {
      questionId: question.id,
      answerId: answer.id
    };
    if(index === -1) {
      this.submission.answers.push(ans);
    } else {
      this.submission.answers.splice(index, 1);
      this.submission.answers.push(ans);
    }
    console.log(this.submission);
  }

TL;DR:如何使单击的无线电输入“选中”?

我将数据 POST 到服务器,问题只是审美问题(用户看不到他选择的内容)。

根据我的搜索,我得出的结论是,我需要为每个输入设置 v-model,但在这种情况下该怎么做?

Radio inputs

【问题讨论】:

    标签: javascript vue.js vuejs2


    【解决方案1】:

    您可以使用:checked 将:radio 标记为选中。

    最简单的方法就是为每个答案添加一个checked 属性并将其绑定到收音机:

    <input type="radio" ... v-bind:checked="answer.checked">
    

    基本上对两种问题类型的inputs(radio 和range)都这样做。

    并调整您的方法以使其在单击时检查:

    updateAnswer(question, answer) {
      question.QuestionAnswers.forEach(a => Vue.set(a, 'checked', false));
      answer.checked = true; // no need to call Vue.set because it is now reactive
    

    上述方法“取消选中”该问题的所有答案,然后将点击的问题标记为选中。

    完整演示:

    new Vue({
      el: '#app',
      data() {
        return {
          submission: {
            email: undefined,
            age: undefined,
            answers: []
          },
          poll: {Questions: [
            {question: "radio question", id: 1, type: 'radio', QuestionAnswers: [
              {id: 1, answer: 'a'}, {id: 2, answer: 'b'}
            ]},
            {question: "range question", id: 2, type: 'range', QuestionAnswers: [
              {id: 1, answer: 'a'}, {id: 2, answer: 'b'}
            ]}
          ]},
          loading: false
        }
      },
      methods: {
        updateAnswer(question, answer) {
          question.QuestionAnswers.forEach(a => Vue.set(a, 'checked', false));
          answer.checked = true; // no need to call Vue.set because it is now reactive
          let index = this.submission.answers.findIndex(q => q.questionId === question.id);
          let ans = {
            questionId: question.id,
            answerId: answer.id
          };
          if (index === -1) {
            this.submission.answers.push(ans);
          } else {
            this.submission.answers.splice(index, 1);
            this.submission.answers.push(ans);
          }
          console.log(this.submission);
        }
      }
    })
    <script src="https://unpkg.com/vue"></script>
    
    <div id="app">
      <label>Ankieta</label>
      <div class="card mb-2" v-for="question in poll.Questions">
        <div class="card-header p-2">{{question.question}}</div>
        <div class="card-body pb-1">
          <div class="form-group" v-if="question.type === 'radio'">
            <div v-for="answer in question.QuestionAnswers">
              <input type="radio" :name="'question'+question.id" v-bind:value="answer.answer" v-on:click.prevent="updateAnswer(question, answer)" v-bind:checked="answer.checked">
              <label v-bind:for="answer.id">{{answer.answer}}</label>
            </div>
          </div>
          <div v-else-if="question.type === 'range'">
            <p class="text-muted mb-4 d-flex justify-content-center">W jakim stopniu zgadzasz się ze stwierdzeniem w pytaniu?<br/>1 - Bardzo się nie zgadzam, 3 - Neutralny/a, 5 - Bardzo się zgadzam<br></p>
            <div class="form-group d-flex justify-content-center">
              <label class="radio-inline d-flex justify-content-center w-100" v-for="answer in question.QuestionAnswers">
                <input type="radio" :name="'question'+question.id" v-bind:value="answer.answer" v-on:click.prevent="updateAnswer(question, answer)" v-bind:checked="answer.checked"><span class="text-muted font-weight-bold m-1">{{answer.answer}}</span>
              </label>
            </div>
          </div>
        </div>
      </div>
    </div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-18
      • 2021-09-23
      • 2013-08-16
      • 1970-01-01
      • 2020-12-30
      • 2018-01-04
      • 2012-03-24
      • 1970-01-01
      相关资源
      最近更新 更多