gpt4 book ai didi

javascript - V-for 单击所有按钮而不是仅单击一个按钮

转载 作者:行者123 更新时间:2023-12-02 23:27:52 25 4
gpt4 key购买 nike

我是 Vue.js 新手。我正在尝试基于数组从 v-for (正确渲染)渲染复选框。

然后我尝试在每个复选框旁边渲染一个按钮,该按钮根据所选索引打开多选。但每次我单击渲染的按钮时,它都会在所有复选框按钮上打开多选。

HTML:

<div>
<label class='label'>countrys:* </label><br><br>
<div
v-for="(country, index) in countries"
class="label"
style="display: inline-block;">
<input
type='checkbox'
value="country">&nbsp
{{country.geos}} &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
<img
class='addIcon'
v-bind="country"
:key="country.index"
style="width: 26px;height: 20px;margin-right: 8px;margin-top: px;margin-left: -25px;margin-bottom:-5px"
src='../../images/createScreen/addClient@2x.png'
@click="makeMarketsActive($event, index)">
<select multiple v-if="!isHidden">
<option
v-for="(market) in country.markets"
v-bind="country">
{{ market }}
</option>
</select>
</div>
</div>

JS:

export default {
name: "Update",
components: {
},
data() {
return {
countries:[
{
"index":0,
"geos":"America",
"markets":['a','b','c','d']
},
{
"index":1,
"geos":"Canada",
"markets":['a','b']
},
"index":2,
"geos":"Africa",
"markets":['x','z']
}
],
isHidden:true
}
},
makeMarketsActive(event, index) {
this.isHidden = !this.isHidden;
}

预期结果:当单击为每个复选框呈现的图像时,我只想查看每个地理区域的市场,而不是所有地理区域。

最佳答案

您也不需要额外的功能

HTML

<div id="app">

<label class='label'>countries:* </label><br><br>
<div v-for="(country, index) in countries" class="label" style="display: inline-block;">
<input type='checkbox' value="country">{{country.geos}}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

<img class='addIcon' v-bind="country" :key="country.index" style="margin-right: 8px;margin-top: px;margin-left: -25px;margin-bottom:-5px" src='https://via.placeholder.com/26x20' v-on:click="country.isVisible = !country.isVisible">
<select multiple v-show="country.isVisible">
<option v-for="(market) in country.markets" v-bind="country" >{{ market }}</option>
</select>
</div>

</div>

JS

new Vue({
el: '#app',
data: {
countries: [{
"index": 0,
"geos": "America",
"markets": ['a', 'b', 'c', 'd'],
"isVisible": false
},
{
"index": 1,
"geos": "Canada",
"markets": ['a', 'b'],
"isVisible": false
}, {
"index": 2,
"geos": "Africa",
"markets": ['x', 'z'],
"isVisible": false
}
]
}
})

关于javascript - V-for 单击所有按钮而不是仅单击一个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56654824/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com