在微信小程序中判断input或者textarean内容为空
直接上代码
wxml:
<view class="molde_one">
<textarea class="opinion_input" type="text" placeholder="您好" value="{{contentInput}}"></textarea>
<button class="opinion_btn" bindtap="smbs" style="width:680rpx">提交反馈</button>
</view>
js:
data: {
contentInput : '',
},
smbs:function (e) {
let content = this.data.contentInput
console.log(content)
if (content == '') {
wx.showToast({
title: '请填写内容',
icon: 'none',
duration: 1500
})
return false
}else {
wx.showModal({
title: '提交成功!',
content: '感谢您的反馈,后续将有工作人员跟踪处理',
showCancel: false ,//去掉取消按钮
confirmText: '确定',
cancelText: '取消',
confirmColor: '#36D5DD'
})
}
}
这样确实判断到Input里没有内容为空了,但是有内容的时候提交他还是报空
这是有内容的时候,他还是报请填写内容。
我们跟contentInput值没有双向绑定,输入时contentInput没有改变
怎么办。。。。
我们要获取到input的value值,给textarea加一个bindinput事件
<textarea class="opinion_input" type="text" bindinput='usernameInput' placeholder="欢迎提出意见和建议,我们的进步离不开您的反馈" value="{{contentInput}}"></textarea>
在输入东西的时候获取到值`
usernameInput:function(e){//获取input里的value值
this.setData({
contentInput: e.detail.value
})
console.log(this.data.contentInput)
},
smbs:function (e) {
let content = this.data.contentInput
console.log(content)
if (content == '') {
wx.showToast({
title: '请填写内容',
icon: 'none',
duration: 1500
})
return false
}else
if (!content == '') {
wx.showModal({
title: '提交成功!',
content: '感谢您的反馈,后续将有工作人员跟踪处理',
showCancel: false ,//去掉取消按钮
confirmText: '确定',
cancelText: '取消',
confirmColor: '#36D5DD'
})
}
}
打印可以看出我们获取到了,然后点击提交
OK,这样就完成了。