当前位置:首页 > VUE

vue实现多填空

2026-01-16 00:07:09VUE

实现多填空功能

在Vue中实现多填空功能可以通过动态绑定和循环渲染来实现。以下是几种常见的实现方式:

动态表单绑定

使用v-for指令动态生成填空输入框,并通过v-model绑定到数组或对象上:

vue实现多填空

<template>
  <div>
    <div v-for="(blank, index) in blanks" :key="index">
      <input v-model="blank.value" :placeholder="`填空 ${index + 1}`" />
    </div>
    <button @click="addBlank">添加填空</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      blanks: [{ value: '' }, { value: '' }]
    }
  },
  methods: {
    addBlank() {
      this.blanks.push({ value: '' })
    }
  }
}
</script>

填空题与文本混合

如果需要在一段文本中嵌入多个填空,可以使用模板字符串和动态组件:

vue实现多填空

<template>
  <div>
    <p v-html="processedText"></p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      text: '{{1}}是中国的首都,{{2}}是最大的城市',
      answers: ['北京', '上海']
    }
  },
  computed: {
    processedText() {
      return this.text.replace(/\{\{(\d+)\}\}/g, (match, p1) => {
        const index = parseInt(p1) - 1
        return `<input v-model="answers[${index}]" />`
      })
    }
  }
}
</script>

使用组件封装

可以创建一个可复用的填空组件:

<template>
  <div class="fill-blank">
    <span v-for="(part, index) in parts" :key="index">
      <span v-if="part.type === 'text'">{{ part.content }}</span>
      <input v-else v-model="answers[part.index]" />
    </span>
  </div>
</template>

<script>
export default {
  props: {
    template: String
  },
  data() {
    return {
      answers: []
    }
  },
  computed: {
    parts() {
      const regex = /\{\{(\d+)\}\}/g
      let parts = []
      let lastIndex = 0
      let match

      while ((match = regex.exec(this.template)) !== null) {
        if (match.index > lastIndex) {
          parts.push({
            type: 'text',
            content: this.template.slice(lastIndex, match.index)
          })
        }

        parts.push({
          type: 'blank',
          index: parseInt(match[1]) - 1
        })

        lastIndex = match.index + match[0].length
      }

      if (lastIndex < this.template.length) {
        parts.push({
          type: 'text',
          content: this.template.slice(lastIndex)
        })
      }

      return parts
    }
  }
}
</script>

使用第三方库

对于更复杂的需求,可以考虑使用专门的表单库如VeeValidate或Vuelidate来增强填空功能:

<template>
  <div>
    <ValidationObserver>
      <div v-for="(blank, index) in blanks" :key="index">
        <ValidationProvider rules="required" v-slot="{ errors }">
          <input v-model="blank.value" />
          <span>{{ errors[0] }}</span>
        </ValidationProvider>
      </div>
    </ValidationObserver>
  </div>
</template>

以上方法可以根据具体需求选择或组合使用,实现灵活的多填空功能。

标签: vue
分享给朋友:

相关文章

vue如何实现注册

vue如何实现注册

实现用户注册功能 在Vue中实现注册功能通常需要结合前端表单和后端API交互。以下是完整的实现方案: 创建注册表单组件 注册表单需要包含用户名、邮箱、密码等基本字段,并进行表单验证: <te…

vue实现用户信息表

vue实现用户信息表

Vue 实现用户信息表的方法 使用 Vue CLI 创建项目 安装 Vue CLI 并初始化项目: npm install -g @vue/cli vue create user-info-tabl…

实现vue

实现vue

安装Vue.js 通过npm安装Vue.js是最常见的方式。确保已安装Node.js和npm,运行以下命令: npm install vue 或者使用CDN直接引入: <script src…

js实现vue

js实现vue

Vue.js 的基本实现方法 在 JavaScript 中实现 Vue.js 的功能,可以通过直接引入 Vue 库或使用现代构建工具(如 Vite 或 Webpack)。以下是几种常见的实现方式:…

vue实现mvc

vue实现mvc

Vue 实现 MVC 模式 Vue 本身是一个 MVVM(Model-View-ViewModel)框架,但可以通过结构调整实现 MVC(Model-View-Controller)模式。以下是具体实…

vue实现pie

vue实现pie

Vue 实现饼图 在 Vue 中实现饼图通常可以通过第三方图表库(如 ECharts、Chart.js 或 Highcharts)来完成。以下是使用 ECharts 和 Chart.js 的两种常见方…