我正在使用 Bootstrap DatePicker control在我的应用程序中。我想弄清楚如何清除该字段的值。我觉得问这个有点愚蠢。老实说,看起来应该是直截了当的。但是,我一直没有成功。我已经尝试过此 SO post 中推荐的方法没有运气。
目前,我有 this JSFiddle .我的相关代码如下所示:
$(function() {
$('#birthday').datetimepicker({ showTodayButton: true });
$('#getDateButton').on('click', function() {
var selectedDate = $('#birthday').data('date');
if (selectedDate) {
selectedDate = selectedDate.format('MM-DD-YYYY');
}
$('#formattedDate').html(selectedDate);
});
$('#clearButton').on('click', function() {
alert('Clear the value in the "birthday" field...');
$('#birthday').data('clear');
// what now?
});
});
好像控件里的功能不起作用了。或者,文档不正确。正如 fiddle 所示,我的“获取日期”功能也不起作用。这就是为什么我觉得有些地方真的不对劲。我是否遗漏了什么或误解了什么?
最佳答案
如果 #birthday
包含日期选择器,执行此操作即可:
$('#clearButton').on('click', function() {
alert('Clear the value in the "birthday" field...');
$('#birthday').data("DateTimePicker").clear();
});
您需要调用clear在 $('#birthday').data("DateTimePicker")
对象上。说明了函数调用 here .
要获取日期,请执行以下操作:
$('#getDateButton').on('click', function() {
var selectedDate = $('#birthday').data('DateTimePicker').date();
if (selectedDate) {
selectedDate = selectedDate.format('MM-DD-YYYY');
console.log("selected date "+ selectedDate);
}
$('#formattedDate').html(selectedDate);
});
关于javascript - 清除 Bootstrap DatePicker 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37240220/