javascript - 使用按钮而不是文本输入时,更改事件上的 jQuery Datepicker 不会被触发

标签 javascript jquery datepicker

我正在尝试使用 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/

相关文章:

javascript - 图像转换错误裁剪

javascript - if else in for循环遍历数组javascript

javascript - 具有样式的 bxslider 自定义寻呼机

javascript - 下拉选择更改

jquery - 由 bootstrap 提供支持的网页中的错误滚动

java - 将 Material 日期选择器值添加到 TextView

javascript - onSelect事件datepicker触发timepicker中minTime值的变化

javascript - 保留纵横比的响应式 iframe

jquery - 更改全日历显示的高度?

javascript - 是什么原因导致 datepicker 无法使用 jquery 在 rails 3.1.3 中加载?