百度小程序-form表單點擊提交,input框內容不會清空


百度小程序與微信小程序相似度90%。微信小程序轉換為百度小程序,部分還是需要人工修改!

做了一個form留言表單,點擊提交之后,input框第一次會清空,但是第二次就不會清空了!

不多說直接上代碼!

.swan文件

<view class="page">
    <form bindreset="formReset">
        <view class="weui-cells__title">基本信息</view>
        <view class="weui-cells weui-cells_after-title">
            <view class="weui-cell weui-cell_input">
                <view class="weui-cell__hd">
                    <view class="weui-label">姓名:</view>
                </view>
                <view class="weui-cell__bd">
                    <input name="name" class="weui-input" placeholder="請輸入你的姓名" type="text" bindinput="getName" />
                </view>
            </view>
            <view class="weui-cell weui-cell_input">
                <view class="weui-cell__hd">
                    <view class="weui-label">電話:</view>
                </view>
                <view class="weui-cell__bd">
<input name="contact" maxlength="11" class="weui-input" placeholder="請輸入聯系電話" type="number" bindinput="getNumber" />
                </view>
            </view>
            <view class="weui-cell weui-cell_input">
                <view class="weui-cell__hd">
                    <view class="weui-label">郵箱:</view>
                </view>
                <view class="weui-cell__bd">
                    <input name="email" class="weui-input" placeholder="請輸入電子郵箱" type="text" bindinput="getEmail"  />
                </view>
            </view>
            <view class="weui-cell weui-cell_input">
                <view class="weui-cell__hd">
                    <view class="weui-label">地址:</view>
                </view>
                <view class="weui-cell__bd">
<input name="addr" class="weui-input" placeholder="請輸入地址信息" type="text" bindinput="getAddress" />
                </view>
            </view>
        </view>
        <view class="weui-cells__title">留言內容</view>
        <view class="weui-cells weui-cells_after-title">
            <view class="weui-cell">
                <view class="weui-cell__bd">
<textarea name="message" class="weui-textarea"  placeholder="請輸入留言內容" style="min-height: 3.3em" bindinput="getMessage" />
                </view>
            </view>
        </view>
        <view class="weui-btn-area">
            <button class="weui-btn" type="primary" disabled="{{falg}}" style="background:#3CA700" form-type="reset">提交</button>
        </view>
</view>
</form>
<!-- 底部版權 S-->
<copyright></copyright>
<!-- 底部版權 E-->
</view>

.js文件

  1 const app = getApp();
  2 Page({
  3     data: {
  4         formDataList: { userName: "", mobileNumber: "", Email: "", Address: "", Message: "" },
  5         falg: true,
  6     },
  7     onLoad: function () {
  8         // 監聽頁面加載的生命周期函數
  9     },
 10     onReady: function () {
 11         // 監聽頁面初次渲染完成的生命周期函數
 12     },
 13     onShow: function () {
 14         // 監聽頁面顯示的生命周期函數
 15         app.setInfo();
 16     },
 17     onHide: function () {
 18         // 監聽頁面隱藏的生命周期函數
 19     },
 20     onUnload: function () {
 21         // 監聽頁面卸載的生命周期函數
 22     },
 23     onPullDownRefresh: function () {
 24         // 監聽用戶下拉動作
 25     },
 26     onReachBottom: function () {
 27         // 頁面上拉觸底事件的處理函數
 28     },
 29     onShareAppMessage: function () {
 30         // 用戶點擊右上角轉發
 31     },
 32     getName: function (e) {
 33         // console.log(e);
 34         this.setData({
 35             'formDataList.userName': e.detail.value,
 36         });
 37         this.falg();
 38 
 39     },
 40     getNumber: function (e) {
 41         // console.log(e);
 42         this.setData({
 43             'formDataList.mobileNumber': e.detail.value,
 44 
 45         })
 46         this.falg();
 47     },
 48     getEmail: function (e) {
 49         // console.log(e);
 50         this.setData({
 51             'formDataList.Email': e.detail.value,
 52         })
 53     },
 54     getAddress: function (e) {
 55         // console.log(e);
 56         this.setData({
 57             'formDataList.Address': e.detail.value,
 58         })
 59     },
 60     getMessage: function (e) {
 61         // console.log(e);
 62         this.setData({
 63             'formDataList.Message': e.detail.value,
 64         })
 65     },
 66     formReset: function (e) {
 67         var that = this;
 68         // console.log(this.data.formDataList)
 69         // console.log('哦豁,form表單被reset了');
 70         swan.request({
 71             url: app.globalData.baseUrl + 'Index/Message',
 72             method: 'get',
 73             data: {
 74                 name: that.data.formDataList.userName,
 75                 contact: that.data.formDataList.mobileNumber,
 76                 email: that.data.formDataList.Email,
 77                 message: that.data.formDataList.Message,
 78                 addr: that.data.formDataList.Address
 79             },
 80             header: {
 81                 genToken: app.globalData.genToken
 82             },
 83             success: function (res) {
 84                 if (res.data.status = 1) {//留言成功
 85                     swan.showToast({
 86                         title: '留言成功',
 87                         icon: 'success',
 88                         duration: 1000,
 89                     });
 90                 }
 91             },
 92             fail: function (err) {
 93                 swan.showToast({
 94                     title: '失敗咯!',
 95                     duration: 1000,
 96                 });
 97             },
 98             complete: function (res) {
 99                 that.setData({
100                     falg: true
101                 });
102             }
103         });
104     },
105     falg: function () {
106         var name = this.data.formDataList.userName;
107         var number = this.data.formDataList.mobileNumber;
108         if (name && number) {
109             this.setData({
110                 falg: false
111             })
112         } else {
113             this.setData({
114                 falg: true
115             })
116         }
117     }
118 
119 });

利用input輸入框監聽事件,獲取到輸入的內容,存儲起來,同時button按鈕,這里是reset,清空表單內容的同時,存儲起來數據並且發送數據到后台!

falg 為按鈕開關狀態控制器,防止惡意點擊提交空數據!


免責聲明!

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



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