收藏
回答

如何实现在某个按钮点击并这个事件操作完成时,如何使输入框的内容自动清空?

我使用this.setData将获取的值的设为空,这样的方式不可行,应该如何解决

回答关注问题邀请回答
收藏

4 个回答

  • 一笑皆春
    一笑皆春
    2023-08-09

    <input  value="{{value}}" />

    然后

    setData({

    value:""

    })

    2023-08-09
    有用
    回复
  • BOBO
    BOBO
    2023-08-09

    this.setData 把对应输入框的值 设置为空即可

    2023-08-09
    有用
    回复
  • 启年
    启年
    2023-08-09

    你写的input组件,应该没有设置value属性,把这个输入框设置一下value属性

    <input  value="{{invitationCode}}" />
    
    2023-08-09
    有用
    回复
  • 贰拾
    贰拾
    2023-08-09

    // pages/addGroup/addGroup.js

    Page({


      /**

       * 页面的初始数据

       */

      data: {

      invitationCode:'',

      openid:'',

      groups:[],

      

      },

      // 监听邀请码

      inputInvitationCode(event){

        this.setData({

          invitationCode:event.detail.value

        })

        console.log(event)

      },

      // 加入小组

      joinGroup(){

        const groupCount = this.data.groupCount;

      if (groupCount >= 6) {

        wx.showToast({

          title: '最多只能加入6个组',

          icon: "none"

        });

        return;

      }

        const invitationCode = this.data.invitationCode

        

        // 判断邀请码是否为空

        if(!invitationCode){

          wx.showToast({

            title: '请输入邀请码',

            icon:"none"

          })

          return

        }

        // 查询数据,找到对应的小组信息


        wx.cloud.database().collection("group").where({

          invitationCode:invitationCode

        }).get({

          success:res=>{

            if(res.data.length === 0){

              wx.showToast({

                title: '邀请码无效',

                icon:'icon'

              })

            }else{

              console.log("否则",res)

              const groupId = res.data[0]._id;

              const openId = wx.getStorageSync('userInfo')

              const openid = openId._openId

              // 将用户openid加入小组成员列表

              wx.cloud.callFunction({

                name:"joinGroup",

                 data:{

                  groupId:groupId,

                  openid:openid

                 },

                 success:res=>{

                   wx.showToast({

                     title: '加入小组成功',

                     icon:"success"

                   })

                   this.setData({

                    groupId: groupId, // 添加该行代码

                    invitationCode:""

                  });

                   this.updateGroups(openid);

                 },

                 fail:err=>{

                   console.error("加入小组失败",err)

                   wx.showToast({

                    title: '加入小组失败',

                    icon:"none"

                  })

                 }

              })

            }

          },

          fail:err=>{

            console.error("查询邀请码失败",err)

            wx.showToast({

              title: '查询邀请码失败',

              icon:"none"

            })

          }

        })

      },

      // 退出小组

      quitGroup() {

        const invitationCode = this.data.invitationCode;

        const openid = this.data.openid;


        // 判断邀请码是否为空

        if (!invitationCode) {

          wx.showToast({

            title: '请输入邀请码',

            icon: 'none'

          });

          return;

        }

       // 查询数据,找到对应的小组信息

       wx.cloud.database().collection("group").where({

        invitationCode: invitationCode

      }).get({

        success: res => {

          if (res.data.length === 0) {

            wx.showToast({

              title: '邀请码无效',

              icon: 'none'

            });

          } else {

            const groupId = res.data[0]._id;

            

            // 从小组的members字段中删除用户的openid

            wx.cloud.callFunction({

              name: 'deletGroup',

              data: {

                groupId: groupId,

                openid: openid,

                invitationCode:invitationCode

              },

              

              success: res => {

                wx.showToast({

                  title: '退出小组成功',

                  icon: 'success'

                });console.log(groupId,openid)

                // 更新页面数据

                this.updateGroups(openid);

              },

              fail: err => {

                console.error('退出小组失败', err);

                wx.showToast({

                  title: '退出小组失败',

                  icon: 'none'

                });

              }

            });

          }

        },

        fail: err => {

          console.error('查询邀请码失败', err);

          wx.showToast({

            title: '查询邀请码失败',

            icon: 'none'

          });

        }

      });

    },


      updateGroups(openid) {

        wx.cloud.database().collection('group').where({

          members: openid

        }).get().then(res => {

          const groupCount = res.data.length

          const groups = res.data.map(group => ({

            groupName: group.groupName,

            invitationCode: group.invitationCode

          }))

          this.setData({

            groupCount,

            groups

          })

        }).catch(err => {

          console.log("查询小组失败", err)

        })

      },



      /**

       * 生命周期函数--监听页面加载

       */

      onLoad(options) {

        const openid = options.openid

        console.log("取值成功",options.openid)

        this.setData({

          openid,

        })

        this.updateGroups(openid);

      },


      /**

       * 生命周期函数--监听页面初次渲染完成

       */

      onReady() {

        

      },


      /**

       * 生命周期函数--监听页面显示

       */

      onShow() {


      },


      /**

       * 生命周期函数--监听页面隐藏

       */

      onHide() {


      },


      /**

       * 生命周期函数--监听页面卸载

       */

      onUnload() {


      },


      /**

       * 页面相关事件处理函数--监听用户下拉动作

       */

      onPullDownRefresh() {


      },


      /**

       * 页面上拉触底事件的处理函数

       */

      onReachBottom() {


      },


      /**

       * 用户点击右上角分享

       */

      onShareAppMessage() {


      }

    })

    2023-08-09
    有用
    回复
登录 后发表内容