微信小程序獲取input值的兩種常用方式

1. bindinput
  • 事件是光標移動發生數據改變,不需要手動執行點擊 。 數據自動獲取
  • input框內使用屬性的方式定義事件名稱
<input  bindinput='getInputValue'  name='price' type='text' placeholder='輸入內容'></input>
  • 在js 文件中定義事件方法獲取數據
    • 其中 e.detail 是獲取 input 數據 其中包含value值, cursor 是獲取光標的位置
getInputValue(e){
  console.log(e.detail)// {value: "ff", cursor: 2}  
}

其他還有三種方法用法一致:在這裏插入圖片描述

2. bindsubmit
  • 攜帶 form 中的數據觸發 submit 事件
  • bindsubmit 事件需要配合按鈕的formType="submit" 操作
  • 設置input的name值來獲取對應的數據
<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;
  }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章