我正在尝试使用 jQuery dtaepicker 制作一个仅按钮日期选择器。
当我将日期选择器附加到输入时,onchange 函数起作用,并且我获得了新的选定值。
但是,当我仅使用按钮或其他东西附加日期选择器时,onchange 事件不会被触发:
<input type="text" class="test"> // works
<i class="fa fa-calendar test"></i> // opens the datepicker as should be, but not fire the change event
$(function() {
$(".test").datepicker({
format: 'yyyy-mm-dd',
onSelect: function(v) {
// also not getting fired
},
}).on("change", function() {
// not getting fired when using button instead of text input
let val = $(this).val();
});
});
最佳答案
我认为您需要添加此行 showOn: 'button'
<input type="text" class="test"> // works
<i class="fa fa-calendar test"></i> // opens the datepicker as should be, but not fire the change event
$(function() {
$(".test").datepicker({
showOn: 'button', // use showOn: 'both' if you want to open the datepicker on icon and the input as well
format: 'yyyy-mm-dd',
onSelect: function(v) {
// This would get fired on changing date on the calendar icon
},
}).on("change", function() {
// not getting fired when using button instead of text input
let val = $(this).val();
});
});
此外,您还可以删除 <i class="fa fa-calendar test"></i>
标签并在 showOn 属性后面放置以下两个 LOC 以显示日历图标
buttonImageOnly: true,
buttonImage: 'http://jqueryui.com/resources/demos/datepicker/images/calendar.gif',
关于javascript - 使用按钮而不是文本输入时,更改事件上的 jQuery Datepicker 不会被触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54485838/