英文:
I have a text field with flatpickr but I don't know how to set date from parameter values
问题
flatpickr.set('defaultDate', start_date);
// 错误:set 不是函数
英文:
I have a text field with flatpickr enabled everything works fine, but I don't know how to set a date to flatpickr date calender from url string parameter.
Below is the code
var start_date = #{@start_date}
const fp = flatpickr('.datetimepicker', {
dateFormat: "d-M-Y h:i K",
enableTime: true,
allowInput: true,
});
flatpickr.set('defaultDate', start_date);
I get javascript error set is not the function
答案1
得分: 1
Instead of using flatpickr.set()
you can use the Flatpickr flatpickr.setDate()
method. (view documentation)
Example:
const fp = flatpickr(".datetimepicker", {
dateFormat: "d-M-Y h:i K",
enableTime: true,
allowInput: true,
});
fp.setDate(new Date("2023-01-01 11:00:00"));
<link href="https://cdnjs.cloudflare.com/ajax/libs/flatpickr/4.6.13/flatpickr.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/flatpickr/4.6.13/flatpickr.min.js"></script>
<input class="datetimepicker" type="text">
英文:
Instead of using flatpickr.set()
you can use the Flatpickr flatpickr.setDate()
method. (view documentation)
Example:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const fp = flatpickr(".datetimepicker", {
dateFormat: "d-M-Y h:i K",
enableTime: true,
allowInput: true,
});
fp.setDate(new Date("2023-01-01 11:00:00"));
<!-- language: lang-html -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/flatpickr/4.6.13/flatpickr.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/flatpickr/4.6.13/flatpickr.min.js"></script>
<input class="datetimepicker" type="text">
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论