微信小程序獲取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


免責聲明!

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



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