微信小程序怎样保存input值让它与下一个input值相加

小程序中,有多个input输入框,每个框上都绑定了bindchange事件,怎样在每输入一个值后保存与后面一个值相加,以此类推,显示在页面


慕森卡
浏览 931回答 1
1回答

慕哥6287543

<view><input value='0' type="number" bindblur="inputB" data-iname='input1' ></input><input value='0' type="number" bindblur="inputB" data-iname='input2' ></input><input value='0' type="number" bindblur="inputB" data-iname='input3' ></input><input value='0' type="number" bindblur="inputB" data-iname='input4' ></input><button bindtap='cal'>总计</button> <text>{{count}}</text></view>&nbsp;&nbsp;Page({data: {count:0,all:{}},cal:function(){var count = 0;var all = this.data.all;for(var i in all){count += Number(all[i]);}&nbsp;this.setData({count: count});console.log(this.data.count, count, all);},inputB:function(e){var all = this.data.all;var iname = e.target.dataset.iname;all[iname] = e.detail.value;this.setData({all:all});},onLoad: function () {&nbsp;},onReady:function(){}})
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript