当前位置:首页 > VUE

vue实现多填空

2026-01-16 00:07:09VUE

实现多填空功能

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

动态表单绑定

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

<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>

填空题与文本混合

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

<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来增强填空功能:

vue实现多填空

<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 实现回复功能 在 Vue 中实现回复功能通常涉及表单提交、数据绑定和列表渲染。以下是实现步骤和代码示例: 数据绑定与表单 <template> <div>…

vue实现导出

vue实现导出

Vue 实现导出功能的方法 在 Vue 项目中实现导出功能通常涉及将数据导出为 Excel、CSV 或其他格式。以下是几种常见的实现方式: 使用 xlsx 库导出 Excel 安装 xlsx 库:…

vue实现必填

vue实现必填

Vue 实现表单必填验证的方法 在Vue中实现表单必填验证可以通过多种方式完成,以下是几种常见的方法: 使用HTML5原生属性 最简单的必填验证可以通过HTML5的required属性实现: &l…

vue scrolltop 实现

vue scrolltop 实现

实现 Vue 中的 scrollTop 在 Vue 中实现滚动到顶部功能可以通过多种方式完成,包括使用原生 JavaScript、Vue 指令或第三方库。以下是几种常见的实现方法: 使用原生 Jav…

vue实现兼容

vue实现兼容

Vue 兼容性实现方案 Vue.js 的兼容性主要涉及对不同浏览器、Vue 版本以及第三方库的支持。以下是常见的兼容性解决方案: 浏览器兼容性 使用 Babel 转换 ES6+ 代码为 ES5 语…

vue实现keepalive

vue实现keepalive

Vue 中实现 keep-alive 在 Vue 中,keep-alive 是一个内置组件,用于缓存动态组件或组件的状态,避免重复渲染和销毁。以下是实现 keep-alive 的几种方法: 基本用法…