首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于对象的Vuejs Vuejs数组

用于对象的Vuejs Vuejs数组
EN

Stack Overflow用户
提问于 2022-11-29 09:54:28
回答 1查看 25关注 0票数 0

我对武伊林有意见。每次按下图片中的框时,都会向数组发送一个对象。可以选择所有的框。

条件是必须选择最多3个框,如果超过3个,我不想发送表单。

下面是按下任何框时运行的代码。

代码语言:javascript
复制
valueSelected(value,index) {
  // console.log(index)
  // console.log(value)
  const i = this.mySelectedValue.indexOf(value)
  // console.log('const i',i)
  if (i === -1) {
    this.mySelectedValue.push(value)
  } else {
    this.mySelectedValue.splice(i, 1)
  }
  const findIndex = this.user.positions.findIndex(v => {
    return v.position === value.position
  })

  if (findIndex === -1) {
    this.user.positions.push(value)
  } else {
    this.user.positions.splice(findIndex, 1)
  }
},
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-11-29 10:02:47

考虑到这就是你所调用的全部功能。您可以在它周围放置一个if,所以如果已经选择了3个选项,那么它将不会触发。我认为这是一条简单的出路。

代码语言:javascript
复制
valueSelected(value,index) {
  // console.log(index)
  // console.log(value)
  const i = this.mySelectedValue.indexOf(value)
  // console.log('const i',i)
  if (i === -1) {
    if(this.mySelectedValue.length < 3){
        this.mySelectedValue.push(value)
    }
  } else {
    this.mySelectedValue.splice(i, 1)
  }
  const findIndex = this.user.positions.findIndex(v => {
    return v.position === value.position
  })

  if (findIndex === -1) {
    if(this.user.positions.length < 3){
        this.user.positions.push(value)
    }
  } else {
    this.user.positions.splice(findIndex, 1)
  }
},
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74612145

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档