admin管理员组文章数量:1291225
Let's say I have a JS ponent - I am using Vue.JS - with a checkbox and associated label in it:
<template>
<input id="field1" type="checkbox">
<label for="field1">
Some label
</label>
</template>
Now, since we designed ponents to be reusable, I want to use it in multiple places of my app, at the same time. Problem: the ID is duplicated, and clicking a checkbox's label checks another checkbox since they share the same ID.
How to solve this problem?
For now I am generating a random hex ID at ponent mount to generate unique ID values, but it feels way too hackish.
Let's say I have a JS ponent - I am using Vue.JS - with a checkbox and associated label in it:
<template>
<input id="field1" type="checkbox">
<label for="field1">
Some label
</label>
</template>
Now, since we designed ponents to be reusable, I want to use it in multiple places of my app, at the same time. Problem: the ID is duplicated, and clicking a checkbox's label checks another checkbox since they share the same ID.
How to solve this problem?
For now I am generating a random hex ID at ponent mount to generate unique ID values, but it feels way too hackish.
Share Improve this question edited Sep 16, 2018 at 20:04 Mickaël asked Sep 14, 2018 at 17:16 MickaëlMickaël 3,9425 gold badges27 silver badges32 bronze badges 10-
If you need the checkbox to do something specific, just use
class
to wire up the event handler and don't bother with theid
– Ryan Wilson Commented Sep 14, 2018 at 17:18 -
The ID is needed for the following
label
tag. I do use Vue'sv-model
to update the data but this was not relevant here. So I'm not sure I understand your advice :/ – Mickaël Commented Sep 14, 2018 at 17:23 - it is not a vue problem and you just shouldn't use the same id on more than one element. – Rafael Quintela Commented Sep 14, 2018 at 17:35
- If you already figured out a way to make the id unique, I don't understand what your problem is. – Ryan Wilson Commented Sep 14, 2018 at 17:37
- 1 You can use this._uid in mounted() and assign this to your id, but _uid is private and might change in the future. Check example codepen.io/anon/pen/RYBjBP – gijoe Commented Sep 14, 2018 at 17:41
4 Answers
Reset to default 9You don't need to use an id
to connect a label
and an input
in this specific case.
This code below achieves the same result without using HTML id
<template>
<label>
<input type="checkbox">
Some label
</label>
</template
You need to dynamically assign IDs as prop using v-bind
. This way, you will be able to set it manually each time you use your ponent.
Component
<template>
<div class="field">
<input v-bind:id="id" type="checkbox"/>
<label v-bind:for="id">{{ label }}</label>
</div>
</template>
<script>
export default {
name: 'checkbox',
props: {
id: {
type: String,
required: true
},
label: {
type: String,
required: true
}
}
}
</script>
**Use case**
<Checkbox id="field1" label="Some text"></Checkbox>
<Checkbox id="field2" label="Another text"></Checkbox>
**Output**
<div class="field">
<input id="field1" type="checkbox">
<label for="field1">Some text</label>
</div>
<div class="field">
<input id="field2" type="checkbox">
<label for="field2">Another text</label>
</div>
[More about props.][1]
You are right. I have a ponent which contains 5 radio buttons. When I apply several these ponents on another view, duplicated id happens
本文标签: javascriptDuplicate ID when using multiple instance of a containerStack Overflow
版权声明:本文标题:javascript - Duplicate ID when using multiple instance of a container - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741530127a2383707.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论