在設計輸入日期欄位,用 HTML5 有 date 的輸入型態可以用,但是若是只想取年份跟月份,藉助 datepicker 就很方便
這回採用 bootstrap-datepicker 這個 plugin
問題:
以一般使用方法沒問題,不過用了 v-model 的 data bind 屬性後,雖然在 UI 看起來無異,但是 Vue 就是讀不到值
解決方法:
在 mounted() 去監聽 datepicker 的 changeDate 事件
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://github.com/uxsolutions/bootstrap-datepicker
const app = new Vue({
el: '#app',
data: {
startDate: '',
},
mounted() {
// listen to changeDate event in mounted()
$('#startDate').datepicker().on(
'changeDate', () => { this.startDate = $('#startDate').val() }
);
}
});
參考連結:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://github.com/uxsolutions/bootstrap-datepicker | |
const app = new Vue({ | |
el: '#app', | |
data: { | |
startDate: '', | |
}, | |
mounted() { | |
// listen to changeDate event in mounted() | |
$('#startDate').datepicker().on( | |
'changeDate', () => { this.startDate = $('#startDate').val() } | |
); | |
} | |
}); |