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

