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

标签 javascript arrays vue.js

我是 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/

相关文章:

javascript - 如何使用 Android 搜索查询(从操作栏中的搜索框)加载最后带有查询的指定 URL?

复杂的字符串和数组声明

vue.js - 如何继承Vue Json

javascript - 从 CLI 生成的新 vue 3 项目的单元测试中出错

javascript - 网站youtube内嵌视频一直在播放

javascript - 如何申请:hover on :after to certain div in CSS

arrays - Swift - 如何将 Any 转换为 Array\Dictionary?

javascript - VueJS - 如何使用 ajax 调用的结果动态初始化模板

javascript - 计数卡 代码 说明

c - c中的字符串数组