如何使用Vue在重复的输入元素上设置input id和label for,使其唯一.JSV2.

<section class="section" v-for="(section, i) in sections">

    <div class="fields">
        <fieldset>
            <input type="checkbox" id="" name="example-a" v-model="section.ex-a">
            <label for="">Example A</label>
        </fieldset>
        <fieldset>
            <label for="">Example B</label>
            <input type="text" id="" name="example-b" v-model="section.ex-b">
        </fieldset>
        <fieldset>
            <label for="">Example C</label>
            <input type="text" id="" name="example-c" v-model="section.ex-c">
        </fieldset>
    </div>

</section>

我希望能够点击标签元素,让它 Select 输入字段.

推荐答案

你可以使用:id="":for="".您需要添加一个js字符串插值,以根据节和索引信息创建唯一的id.例如:

<div class="fields">
    <fieldset>
        <input type="checkbox" :id="'section'+i+'example-a'" name="example-a" v-model="section.ex-a">
        <label :for="'section'+i+'example-a'">Example A</label>
    </fieldset>
    <fieldset>
        <label for="'section'+i+'example-b'">Example B</label>
        <input type="text" :id="'section'+i+'example-b'" name="example-b" v-model="section.ex-b">
    </fieldset>
    <fieldset>
        <label :for="'section'+i+'example-c'">Example C</label>
        <input type="text" :id="'section'+i+'example-c'" name="example-c" v-model="section.ex-c">
    </fieldset>
</div>

例如,对于第一个fieldset,绑定:id="'section'+i+'example-a'"将渲染id="section-0-example-a"作为其插值'section' + the index of the array + 'example-a'

Vue.js相关问答推荐

Vuetify从同一方向旋转传送带下一个项目和上一个项目

当props 的值改变时如何更新类'

如何用其他组件编译Vue模板?

Vue3组合-如何在组件卸载时注销回调

Nuxt 3 I18N浏览器检测不起作用

如何对将 DOM 元素附加到 HTML 正文的 Vue 组件进行单元测试?

VueJS中的绑定函数含义

在重复内容区域中添加

Vue.js 如何计算总数?

如何将 axios/axios 拦截器全局附加到 Nuxt?

Javascript/vue.js 接收json

我可以使用react路由创建别名路由吗?

Vuetify Jest 未知自定义元素

vue 3 使用 Vuex 和路由的服务器端渲染

如何从 vue 组件调用 App.vue 中的方法

找不到模块 'babel-core' 但已安装 @babel/core

vuejs 配置:使用全局变量?

VueJS错误避免直接改变props

mount() 仅在组件 Vue.js 上运行一次

单击 v-select 项目时如何获取对象而不是单个值?