小程序相同name属性的input输入框怎么取值呢?
发布于 6 年前 作者 wangming 14652 次浏览 来自 问答

类似php页面很多个<input name=“user_name[]”>,获取到一个user_name的数组

5 回复

取值的时候循环  取到值就压进数组里,最后值都在数组里

name可以不取一样的,比如name=“yourname_{{index}}”,然后在js里再去处理就好了。

嗯,是不支持的。但是我要循环添加很多个相同name的input,要怎么样取到所有的值呢,求助啊!!

实现了你需要的,可直接粘贴代码测试

<button bindtap=“addInput”>添加Input</button>

<button bindtap=“removeInput”>删除Input</button>

<view style=“border:1rpx solid #eee;”>

  <block wx:for="{{Input}}">

    <input style=“background-color:#eee;border:1rpx solid #000;margin-bottom:20rpx;padding-left:20px” type=“text” value="{{item}}"  bindinput=“inputChange” data-index="{{index}}" />

  </block>

</view>

<button bindtap=“consoleARR”>查看所有Input值</button>


Page({

  data: {

    Input: [], // 用来添加/删除组件的数组

    arr:[]  // 用来存放组件value的数组

  },

  addInput: function () {

    this.data.Input.push((this.data.Input.length + 1).toString())

    this.setData({ Input: this.data.Input })

  },

  removeInput: function () {

    this.data.Input.pop();

    this.setData({ Input: this.data.Input })

  },

  inputChange:function(e){

    //console.log(e.target.dataset.index,e.detail.value);

    var index=e.target.dataset.index;

    var value=e.detail.value;

    this.data.arr[index]=value;

    this.setData({arr:this.data.arr})

    //console.log(this.data.arr)

  },

  consoleARR:function(){

    console.log(this.data.arr)

  }

})

小程序不支持

回到顶部