收藏
回答

小程序相同name属性的input输入框怎么取值呢?

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

回答关注问题邀请回答
收藏

5 个回答

  • 炒饭²º²¹
    炒饭²º²¹
    2017-05-08

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


    <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)

      }

    })


    2017-05-08
    有用 1
    回复
  • 约修亚
    约修亚
    2017-05-08

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

    2017-05-08
    有用
    回复 1
    • chen
      chen
      2020-09-24
      如何利用name来区分啊
      2020-09-24
      回复
  • 炒饭²º²¹
    炒饭²º²¹
    2017-05-08

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

    2017-05-08
    有用
    回复
  • phoenixMarco
    phoenixMarco
    2017-05-08

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

    2017-05-08
    有用
    回复
  • 超哥[天啊][天啊][天啊]
    超哥[天啊][天啊][天啊]
    2017-05-08

    小程序不支持

    2017-05-08
    有用
    回复
登录 后发表内容