微信小程序中用到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 }) } ); } } })