微信小程序判斷input是否為空


微信小程序中用到input值時候,判斷其內容是否為空,可以用if-else判斷內容的length,也可以給input加點擊事件,判斷其內容;以下是我解決問題的過程
wxml代碼

  <view class="name">
    <text>姓名</text>
    <input type='text' placeholder='收貨人姓名' value="{{userName}}" bindblur='username'></input>
  </view>
  <view class="name">
    <text>電話</text>
    <input type='number' placeholder='收貨人手機號' value="{{phone}}" bindblur='phone'></input>
  </view>
  <view class="preserve" bindtap='sever'>
    <text>保存</text>
  </view>
Page({
  data: {
    username: '',
    phone: '',
  }
sever: function() {
    let name = this.data.username
    let phone = this.data.phone
    if (name == '') {
      wx: wx.showToast({
        title: '請輸入姓名'
      })
      return false
    }
    else if (phone == '') {
      wx: wx.showToast({
        title: '請輸入手機號'
      })
      return false
    }
    else {
      app.postData("member.address/add", {
        name: name,
        phone: phone
      }, "POST").then(
        res => {
          wx.showToast({
            title: '保存成功',
            icon: 'succes',
            duration: 1000,
            mask: true
          })
        }
      );
    }
  }
  })


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM