【问题标题】:Clicking radio button selects radio in all groups in Bootstrap-Vue单击单选按钮选择 Bootstrap-Vue 中所有组中的单选
【发布时间】:2021-07-16 14:07:39
【问题描述】:

Bootstrap-vue 动态多项目行单选按钮选择未选中单个单选项目

这是我的 Vue data 属性和列表数组:

data() {
    return {
      dataList: [],
      question_option: '',
    }

我在 Bootstrap-Vue 中根据 dataList 动态显示我的单选按钮组。

  <ul class="question_list" v-for="(item, index) in dataList" :key="index">
    <li>
      <p><b>{{ index + 1 + '. ' + item.question }}</b></p>
      <b-form-group :id="index.toString()" :name="index.toString()">
        <b-form-radio-group
          v-model="question_option"
          :options="item.qOptions"
          :name="index.toString()"
          stacked
        />
      </b-form-group>
      <b-card-text class="mt-1 mb-0">
        <p><b>Hints:</b> <span v-if="question_option !== ''" v-html="item.hints"></span></p>
      </b-card-text>
    </li>
  </ul>

如果我选择一个单选选项,那么它会在每个单选组中被选中。

我只想通过点击选择一个单选项目。我该如何解决这个问题?

【问题讨论】:

  • 它的发生是因为它们都有相同的值 question_option v-model="question_option"

标签: vue.js bootstrap-4 bootstrap-vue radio-group


【解决方案1】:

您遇到的问题是您如何在b-form-group-component 上使用v-model:

<b-form-radio-group
  v-model="question_option"
  :options="item.qOptions"
  :name="index.toString()"
  stacked
/>

每个项目显示一个单选组,然后将所有这些组的结果存储在相同的单个数据变量中:question_option。

为了解决这个问题,您需要将所选选项存储在一个数组中,这样您就可以单独跟踪每个问题的选择。您可以为其创建一个新数组,也可以为存储在dataList 中的item 对象添加一个属性以供使用,这对您来说更有意义。

在下面的示例中,我进行了以下更改:

  • 我为dataList 中的每个项目添加了question_option 属性
  • 将v-model="question_option" 更改为v-model="item.question_option"
  • 将“提示”也更改为引用item.question_option
  • 在“提示”下方添加了一个跨度,以显示给定问题的选择,作为示例以显示它们正确存储

示例

new Vue({
  el: '#app',
  data() { return {
    dataList: [
      {
        question: 'The first question',
        qOptions: [ { text: 'Option 1', value: 1 },
          { text: 'Option 2', value: 2 },
          { text: 'Option 3', value: 3 },
          { text: 'Option 4', value: 4 }],
        hints: 'First question hint',
        question_option: '', // New option property for selection storage
      },
      {
        question: 'The second question',
        qOptions: [ { text: 'Option 1', value: 1 },
          { text: 'Option 2', value: 2 },
          { text: 'Option 3', value: 3 },
          { text: 'Option 4', value: 4 }],
        hints: 'Second question hint',
        question_option: '', // New option property for selection storage
      },
    ],
  }},
})
<link type="text/css" rel="stylesheet" href="//unpkg.com/bootstrap@4/dist/css/bootstrap.min.css" /><link type="text/css" rel="stylesheet" href="//unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.min.css" /><script src="//unpkg.com/vue@latest/dist/vue.min.js"></script><script src="//unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.min.js"></script>

<div id="app">
  <ul class="question_list" v-for="(item, index) in dataList" :key="index">
    <li>
      <p><b>{{ index + 1 + '. ' + item.question }}</b></p>
      <b-form-group :id="index.toString()" :name="index.toString()">
        <!-- Replace "question_option" with "item.question_option" -->
        <b-form-radio-group
          v-model="item.question_option"
          :options="item.qOptions"
          :name="index.toString()"
          stacked
        />
      </b-form-group>
      <b-card-text class="mt-1 mb-0">
        <!-- Replace "question_option" with "item.question_option" -->
        <p><b>Hints:</b> <span v-if="item.question_option !== ''" v-html="item.hints"></span></p>
      </b-card-text>
      <!-- Added a span to show selection, for clarity -->
      <b-card-text class="mt-1 mb-0">
        <p><b>Selected:</b> <span>{{ item.question_option }}</span></p>
      </b-card-text>
    </li>
  </ul>
</div>

【讨论】:

    【解决方案2】:

    首先,您已通过映射重新分配数据对象。这是代码

    dataFormatedList (data, userReviewList) {
    return data.map(item => {
        // Options
        const mySplitP = item.options.split('<p>')
        const mySplitPClose = mySplitP[1].split('</p>')[0]
        const mySplitSlash = mySplitPClose.split('///')
        const correctAnsIndex = mySplitSlash.findIndex(element => element.includes("@@"))
        const mySplitAtTheRate = mySplitSlash[correctAnsIndex].split('@@')[1]
        mySplitSlash.splice(correctAnsIndex, 1, mySplitAtTheRate)
        // Question
        const questionSplit = item.question.split('</p>')[0]
        const question = questionSplit.split('<p>')[1]
    
        const myOption = mySplitSlash.map((item, index) => {
            const is_correct = index === correctAnsIndex ? true : false
            const qOptions = {
            text: item,
            value: index,
            is_correct: is_correct,
            bg_color: 'radio_option_default_custom_design',
            text_color: 'text-primary',
            }
            return Object.assign({}, qOptions)
        })
        const questionOptions = {
            qOptions: myOption,
            question: question,
            correctAnsIndex: correctAnsIndex
        }
        return Object.assign({}, item, questionOptions)
    })
    

    }

    在这里循环

              <ul class="question_list" v-for="(item, index) in dataList" :key="index">
                <li>
                  <div v-for="(qItem, index2) in item.qOptions" :key="index2">
                    <div class="radio_option_bar">
                      <p :class="qItem.bg_color"><b-form-radio :name="'question_option' + index" v-model="question_option[index]" :value="qItem.value" @change="itemSelected(qItem, item)"><span :class="qItem.text_color">{{ qItem.text }}</span></b-form-radio></p>
                    </div>
                  </div>
                  <br>
                </li>
              </ul>
    

    现在你必须使用 onchange 方法:

    itemSelected (option, item) {
      if (option.is_correct) {
        option.bg_color = 'radio_option_correct_custom_design'
        option.text_color = 'text-success'
      } else {
        option.bg_color = 'radio_option_incorrect_custom_design'
        option.text_color = 'text-danger'
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-15
      • 2017-07-24
      • 1970-01-01
      • 2012-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多