目录
  • 效果图
  • wxml代码
  • wxss代码
  • index.js代码
  • 总结

效果图

微信小程序比较两个数大小的实现方法

微信小程序比较两个数大小的实现方法

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})  
        },
        })

总结

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。