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;
}