> 文章列表 > 微信小程序-1:比较两数的大小

微信小程序-1:比较两数的大小

微信小程序-1:比较两数的大小

程序来源》微信小程序开发教程(第二章) 主编:黄寿孟、易芳、陶延涛 

ISBN: 9787566720788

程序运行结果:

<!--index.wxml-->

<view class="container">

<text>第一个数字:</text>

<input id="num1" type="number" bindchange="num1change" />

</view>

<view class="container">

  <text>第二个数字:</text>

<input id="num2" type="number" bindchange="num2change" />

</view>

<button bindtap="compare">确定</button>

<text>比较结果:{{result}}</text>

 


// index.js

Page({

  num1:0,

  num2:0,

  num1change:function(e){

    this.num1=Number(e.detail.value)

    console.log("第1个数为:"+this.num1)

  },

  num2change:function(e){

    this.num2=Number(e.detail.value)

    console.log("第2个数为:"+this.num2)

  },

  compare:function(){

    var str="两数相等"

    if(this.num1>this.num2){

      str="第1个数大"

    }else if(this.num1<this.num2){

      str="第2个数大"

    }

    this.setData({

      result:str

    })

  }

})