微信小程序点击按钮动态添加输入框,点击步进器按钮获取输入框的值进行加减计算--不使用表单

微信小程序实现动态添加或想删除输入框,参考wx:for列表渲染

搭配vantweapp使用,提高开发效率(例如已实现的步进器)
Stepper 步进器https://youzan.github.io/vant-weapp/#/stepper

关键:

  1. wx:for列表渲染每个item,达到增加删除的效果
  2. 绑定date-index自定义数据,作为下标传递
  3. 充分利用数据绑定和中间变量
  4. 事件event传参来获取确定的按钮或数据

实现效果:

在这里插入图片描述

解决办法:

wxml代码:

<view wx:for="{{inputList}}" wx:key="id">
	<view>序号{{index+1}}</view>
	<!-- 步进器 -->
	<van-stepper value="{{ 1 }}" data-index="{{index+1}}" bind:change="numOnChange" />
	<!-- 进价 -->
	<text>进价¥</text>
	<input placeholder="填入进价{{index+1}}" data-index="{{index+1}}" 
	bindinput="inpPurchase" value="{{item.purchaseValue}}"/>
	<!-- 小计 -->
	<view data-index="{{index+1}}">小计¥{{item.sell}}</view>
	<!-- 添加删除按钮 -->
	<button data-index="{{index+1}}"  bindtap="addmore" type="primary">添加</button>
	<button data-index="{{index+1}}" bindtap="delmore">删除</button>
</view>

JavaScript代码:

Page({
  /**
   * 页面的初始数据
   */
  data: {
    inputList:  [{
      value:1,//步进器的值
      purchaseValue:null,//进价输入框的值
      sell:0//小计view的值
    }],
    tempinpPurchase:0,//进价临时值
  },
  //增加按钮
  addmore(e) {
    console.log("增加")
    console.log(e)
    //简写变量书写
    const {inputList} = this.data
    const {dataset: {index}} = e.currentTarget
    //splice方法来添加对象
    //第一个参数是开始的下标,第二个参数是零为添加操作,第三个参数是添加的内容
    inputList.splice(index, 0, {ghsValue:this.data.ghsValue,value:1,purchaseValue:null,sell:0})
    //更新列表
    this.setData({
      inputList
    })
  },
  //步进器改变事件
  numOnChange(e){
    console.log(e)
    const {inputList} = this.data
    const {dataset: {index}} = e.currentTarget
    //e.detail步进器的值
    console.log(e.detail)
    //计算值=数量*中间值
    console.log(Number(e.detail)*(this.data.tempinpPurchase))
    console.log(this.data.inputList[index-1].sell)
    //计算的小计赋值给inputList存储起来
    inputList[index-1].sell=Number(e.detail)*(this.data.tempinpPurchase)
    //数量赋值给inputList存储起来
    inputList[index-1].value=Number(e.detail)
    //更新列表
    this.setData({
      inputList
    })
  },
  //输入进价事件
  inpPurchase(e){
    const {inputList} = this.data
    const {dataset: {index}} = e.currentTarget
    console.log(index)
    //获取输入的值
    console.log("输入的值:"+e.detail.value)
    //将获取的输入值赋值给inputList存储起来
    inputList[index-1].purchaseValue=e.detail.value
    //更新中间变量tempinpPurchase
    this.setData({
      tempinpPurchase:e.detail.value
    })
    console.log(this.data.tempinpPurchase)
    //计算出来的值
    console.log(inputList[index-1].sell)
    //计算出来的值=中间变量*数量
    inputList[index-1].sell=this.data.tempinpPurchase*Number(inputList[index-1].value)
    //更新列表
    this.setData({
      inputList:inputList
    })
  },
})

app.json代码:

"usingComponents": {
  "van-stepper": "@vant/weapp/stepper/index"
}

如果对vantweapp这个微信小程序UI库安装不熟悉,给我评论留言吧
优化更新:https://blog.csdn.net/qq_44776950/article/details/107889137

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要实现在微信小程序点击按钮出现文本输入框,可以使用`<input>`组件来实现。下面是一个示例代码: ```html <view> <button bindtap="showInput">点击输入</button> <input type="text" wx:if="{{showInputBox}}" bindinput="onInput" placeholder="请输入内容" value="{{inputValue}}"/> </view> ``` 在`<button>`组件中,我们绑定了一个`showInput`方法,在这个方法中,我们可以设置一个变量`showInputBox`来控制文本输入框的显示和隐藏。 在`<input>`组件中,我们设置了`wx:if="{{showInputBox}}"`来控制组件的显示和隐藏,同时,我们绑定了一个`onInput`方法,用来处理文本输入框中的内容。在`<input>`组件中,我们还设置了一个`value`属性,用来保存用户输入的内容。 下面是示例代码的js部分: ```javascript Page({ data: { inputValue: '', showInputBox: false }, showInput: function () { this.setData({ showInputBox: true }) }, onInput: function (e) { this.setData({ inputValue: e.detail.value }) } }) ``` 在这个示例代码的js部分中,我们定义了一个`Page`对象,并在`data`属性中定义了两个变量`inputValue`和`showInputBox`,分别用来保存用户输入的内容和控制文本输入框的显示和隐藏。 在`showInput`方法中,我们将`showInputBox`设置为`true`,从而显示文本输入框。 在`onInput`方法中,我们将用户输入的内容保存在`inputValue`变量中。 最后,我们需要在`<button>`组件中绑定`showInput`方法,在`<input>`组件中绑定`onInput`方法,这样就可以实现在微信小程序点击按钮出现文本输入框的功能了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值