小程序登錄時如何獲取input框中的內容


  最近寫小程序項目遇到一些問題,今天整理下這些問題的解決方法,希望對用戶有幫助。下面是登錄頁,點擊登錄時獲取input框中的值,

效果如下:
  

wxml布局如下:

<view >
      <input type="text" placeholder-style="color:#fff;" bindinput="userNameInp" placeholder="請輸入賬號" />
</view>
<view >
      <input  password placeholder-style="color:#fff;" bindinput="usePasswordInp" placeholder="請輸入密碼"/>
</view>
 <button class="loginBtn" bindtap='loginFn'>登錄</button>

js代碼如下:
const app = getApp();
Page({

  /**
   * 頁面的初始數據
   */
  data: {
    userName: "",
    passWord: "",
  },
  //監聽輸入的賬號
  userNameInp: function (e) {
    this.data.userName = e.detail.value;
  },
  //監聽輸入的密碼
  usePasswordInp: function (e) {
    this.data.passWord = e.detail.value;
  },
  //登錄
  loginFn: function () {
    var that = this;
    if (that.data.userName.length == 0 || that.data.passWord.length == 0) {
      wx.showToast({
        title: '賬號或密碼為空',
        icon: 'loading',
        duration: 2000

      })
    } else {
      wx.showLoading({
        title: '登錄中...',
      })
      wx.request({
        url: 'https://localhost:8180/exam/login',
        data: {
          username: that.data.userName,
          password: that.data.passWord
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默認值
        },
        method: 'post',
        success: function (res) {
          wx.hideLoading();
          wx.removeStorageSync('sessionid');
          // console.log('登錄成功', res.data.data);
          if (res.data.code == "0000") {
            wx.showToast({
              title: '登錄成功',
              icon: 'success',
              duration: 1000
            })
            wx.setStorageSync('sessionid', res.header['Set-Cookie']); //保存Cookie到Storage
            wx.setStorage({
              key: 'myData',
              data: res.data.data
            })
            wx.redirectTo({
              url: '/pages/index/index',
            })
          } else {
            wx.showToast({
              title: '登錄失敗',
              icon: 'none',
              duration: 2000
            })
          }
        },
        fail: function (e) {
          wx.showToast({
            title: '服務器出現錯誤',
            icon: 'none',
            duration: 2000
          })
        }

      })
    }

  },

  //跳轉到忘記密碼頁面
  onTapDayWeather() {
    wx.navigateTo({
      url: '/pages/updatepwd/updatepwd',
    })
  },

  /**
   * 生命周期函數--監聽頁面加載
   */
  onLoad: function (options) {

  },


})

  


免責聲明!

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



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