wxml渲染数组 数组删除元素 wxml显示错误
发布于 6 年前 作者 liaona 12484 次浏览 来自 官方Issues
<block wx:for="{{labels}}" wx:key="{{index}}">
  <input  maxlength="{{maxlength}}" placeholder="{{placeholder}}" placeholder-class="fontsize-20 font-color"
class="orange font-color" data-index="{{index}}"  bindinput="input"/>
</block>

 data:{
    saying:"",
    labels:[""//输入标签 最多8个
  },
mmethods:{
  input:function(e){
      let labels=this.data.labels
      const  label=lrtrim(e.detail.value) //用户输入的label
      const index=e.currentTarget.dataset.index //label的索引
      

      if(label==""){
        console.log(`删除索引${index}`)
        console.log(`删除前:${labels}`)
        labels.splice(index,1)
        console.log(`删除后:${labels}`)
      }else{
        console.log(`增加索引${index}`)
        console.log(`增加前:${labels}`)
        labels[index]=label
        console.log(`增加后:${labels}`)
      }
      if(!labels.includes('')&&labels.length!=8){
        labels.push('')
      }
        this.setData({
          labels:labels
        })
      return label
    }
}

bug截图:

wxml渲染数组元素不对

3 回复

补充:截图是数组渲染后的结果。数组值是1,3,4,5,7,’’。渲染结果明显不对。我试了多次

从你打印来看,没啥毛病啊,删除的是索引是4 ,应该是 把 6 删了啊 有啥问题?

input 里加上 value="{{item}}"

回到顶部