yusdddd_
2022-04-14 11:47
采纳率: 100%
浏览 602
已结题

v-model的值怎么实现字符拼接?或者怎么在对象中定义一个数组?

问题遇到的现象和发生背景

想要实现一个通过click生成多个题目的效果,每个题目有不同的v-model,每个题目是一个对象,我在数组中定义了多个对象,对象中定义了
四个option, 想要在v-model中实现 v-model="item.'option'+index",应该怎么实现?

我在数组中定义了对象,又在这个对象里面可以定义数组吗?我定义了之后想加元素进去显示数组不存在类型,我应该怎么实现比较好?

问题相关代码,请勿粘贴截图

HTML部分代码

      <!-- 单选题 -->
      <div class="danxuan main_workType" v-for="(item,index1) in oneChsObject" :key="index1">
        <div class="quesType">{{index1+1}}.单选题</div>
          <el-input
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 4}"
            placeholder="请输入题目内容"
            v-model="item.content">
          </el-input>
        <div class="danxuan-options">   

          <!-- A选项 -->
          <div class="option" v-for="(item,index) in oneChsObject[index1].oneChsOptionsCount" :key="index">
              <el-button round class="danxuan-chose">{{chsOptions[index]}}</el-button>
              <el-input v-model="item.oneChsOption1" placeholder="请输选项A的内容" class="danxuan-input"></el-input>
            </div>
          </div>

        <div class="choose_Btn">
          <span class="addBtn"><el-button type="primary" icon="el-icon-plus" circle @click="oneChsOptionAdd(index1)"></el-button>增加选项</span>
          <span class="delBtn"><el-button type="danger" icon="el-icon-delete" circle @click="oneChsReduce(index1)"></el-button>删除选项</span>
        </div>
        <div class="oneChs-answer">
          <el-select v-model="oneChs_answer" placeholder="请选择该题答案">
            <el-option
              v-for="option in chsOptions"
              :key="option"
              :label="option"
              :value="option">
            </el-option>
          </el-select>
        </div>
      </div>

JS:点击题目增加的方法:

      /* 题目数增加的方法 */
      oneChsCountAddBtn(){
        this.oneChsCount+=1
        let objNum
        objNum = this.oneChsCount
        var newObject = {}
        newObject.num = objNum
        newObject.title = "oneChs_title_"+objNum
        newObject.content = "oneChs_content_"+objNum
        newObject.oneChsOptionsCount = 4
        newObject.oneChsOptions[0] = "oneChs_options_1_"+objNum.toString
        newObject.oneChsOption1 = "oneChs_options_1_"+objNum
        newObject.oneChsOption2 = "oneChs_options_2_"+objNum
        newObject.oneChsOption3 = "oneChs_options_3_"+objNum
        newObject.oneChsOption4 = "oneChs_options_4_"+objNum
        newObject.options_answer = "oneChs_answer_"+objNum
        this.oneChsObject.push(newObject)
      },
运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果

想要达到每个题目中的选项都有不同的v-model

2条回答 默认 最新

相关推荐 更多相似问题