想要实现的效果如下:
<!-- wxml -->
<view class="tr">
<view class="td">第一个数</view>
<view class="td"><input class="weui-input" type="number" maxlength="10" bindinput="cpa1" placeholder="输入数字" /></view>
<view class="td">无</view>
</view>
<view class="tr">
<view class="td">第二个数</view>
<view class="td"><input class="weui-input" type="number" maxlength="10" bindinput="cpa2" placeholder="输入数字" /></view>
<view class="td">无</view>
</view>
<view class="tr">
<view class="td">前两个数之和</view>
<view class="td">{{cpa3}}</view>
<view class="td">无</view>
</view>
<view class="tr">
<view class="td">前两个数的平均</view>
<view class="td">{{cpa4}}</view>
<view class="td">无</view>
</view>
</view>
/*js代码
*/
Page({
/**
* 页面的初始数据
*/
data: {
cpa1: '',
cpa2: '',
cpa3: '',
cpa4: '',
},
cpa1: function (e) {
this.setData({
cpa1: e.detail.value,
})
},
cpa3: function (e) {
this.setData({
cpa3: this.cpa1+this.cpa2,
})
},
cpa4: function (e) {
this.setData({
cpa4: this.cpa4/2,
})
},
代码不报错,但是不出结果,请各位大佬指点一下,js里面的代码该怎么写?
看代码片段 https://developers.weixin.qq.com/s/TXTHIomU7NAM
a+b/2, 应添加一对括号修正为(a+b)/2
需要在上面2个输入框绑定事件,值改变时,自己写方法,来计算就可以了
这是不使用wxs的写法: