微信小程序求两个数之和
时间: 2023-05-28 13:04:40 浏览: 304
以下为一个简单的微信小程序,可以输入两个数字并计算它们的和:
```html
<!--index.wxml-->
<view class="container">
<view class="input-container">
<input type="number" placeholder="请输入第一个数字" bindinput="onInputNumber1"/>
<input type="number" placeholder="请输入第二个数字" bindinput="onInputNumber2"/>
</view>
<view class="result-container">
{{result}}
</view>
</view>
```
```js
//index.js
Page({
data: {
number1: 0,
number2: 0,
result: 0
},
onInputNumber1: function(event) {
this.setData({
number1: parseInt(event.detail.value)
});
this.calculateResult();
},
onInputNumber2: function(event) {
this.setData({
number2: parseInt(event.detail.value)
});
this.calculateResult();
},
calculateResult: function() {
var result = this.data.number1 + this.data.number2;
this.setData({
result: result
});
}
})
```
在这个小程序中,我们使用了两个输入框来输入两个数字,并在输入时触发了相应的事件。在事件中,我们通过 `setData` 方法更新了页面数据,并重新计算了结果。最终的结果会在页面中显示出来。
阅读全文
相关推荐
















