微信小程序获取input值的两种常用方式

微信小程序-获取input值的两种方法

 

1、bindinput

<input  bindinput='getInputValue'  name='price' type='text' placeholder='输入内容'></input>

其中 e.detail 是获取 input 数据 其中包含value值, cursor 是获取光标的位置。

getInputValue(e){
  console.log(e.detail)// {value: "ff", cursor: 2}  
}
2. bindsubmit
复制代码
<form bindsubmit='loginForm'>
  <text class='login-title'>用户登录:</text>
  <input type='text' name='username' placeholder="请输入用户名"></input>
  <input type='password' name='password' placeholder="请输入账号密码"></input>
  <view class='ligin-button'>
    <button formType="submit" type='primary'>点击提交</button>
    <button formType="reset" type='primary'>重置数据表单</button>
  </view>
</form>
复制代码

在js 中获取数据,通过data.detail.value获取数据,获得的是json对象数据键值对一一对应

loginForm: function(data) {
    console.log(data.detail.value)//  {username: "hgj", password: "fsdfsd"}
    var username = data.detail.value.username
    var password = data.detail.value.password;
}

转载自:https://blog.csdn.net/qq_39043923/article/details/89334077

原文地址:https://www.cnblogs.com/guohu/p/12827564.html