zoukankan      html  css  js  c++  java
  • 小程序登录时如何获取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) {
    
      },
    
    
    })
    

      

  • 相关阅读:
    JavaScript没有块级作用域
    JavaScript数据类型
    接口
    Linux用户与组别的操作
    解决 macOS 下 SSH 空闲一段时间后自动断开
    Excel 系列一 之 账户类长数字打开不截断与完全显示
    怎样花两年时间去面试一个人
    北京大学硕士学位论文模板
    Mysql: 图解 inner join、left join、right join、full outer join、union、union all的区别
    刷 LeetCode 时再学习 Python 中引用
  • 原文地址:https://www.cnblogs.com/lvxisha/p/11428767.html
Copyright © 2011-2022 走看看