微信小程序比較兩個數大小的實現方法
更新時間:2022年03月24日 09:13:24 作者:底層的渣渣
最近在工作中遇到一個需求,可以自動對比兩個數的大小,下面這篇文章主要給大家介紹了關于微信小程序比較兩個數大小的實現方法,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
效果圖


wxml代碼
<!--index.wxml-->
<view class="demo-box">
<text class="title">請輸入第一個數字:</text>
<input type="number" bindchange='num1change'/>
</view>
<view class="demo-box">
<text class="title">請輸入第二個數字:</text>
<input type="number" bindchange='num2change'/>
</view>
<button bindtap='compare'>比較大小</button>
<view class="demo-box">
<text class="title">比較結果為:{{result}}</text>
</view>wxss代碼
/**index.wxss**/
.demo-box{
margin: 50rpx;
}
input{
width: 600rpx;
margin-top: 20rpx;
border-bottom: 4rpx solid #cccc;
}
button{
margin: 50rpx;
}
button{
color: aliceblue;
background-color: #369;
letter-spacing: 12rpx;
}
index.js代碼
Page({
data: {
result:''
},
num1:0,//保存一個數字
num2:0,
num1change:function(e){
this.num1 = Number(e.detail.value)
console.log("第一個數為:"+this.num1)
},
num2change:function(e){
this.num2 = Number(e.detail.value)
console.log("第二個數為"+this.num2)
},
compare:function(e){
var str='倆數相等'
if(this.num1 > this.num2){
str = '第一個數大大大大'
}else if (this.num1<this.num2){
str = '第二個數大大大'
}
this.setData({result:str})
},
})
總結
到此這篇關于微信小程序比較兩個數大小的文章就介紹到這了,更多相關微信小程序比較數大小內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

