KendoUI datetime picker - Calling an on click function when previous/next month is selected

烈酒焚心 提交于 2021-01-29 10:11:46

问题


I'd like to trigger an on click event when the user clicks the previous/next month buttons on the Kendo UI date time picker.

The documentation tells me there isn't an event that is triggered when this happens, so I need to make an on click event.

The buttons don't have id's but do have unique classes: k-nav-prev for the previous month button and k-nav-next for the next month button.

However, when I try to put an alert for the on click event for those classes nothing happens. Would anyone know what I am doing wrong, or if there is a better way to trigger an event when these buttons are clicked?

I have attached a code sample. Thanks for any help.

$(document).ready(function() {
  // create DateTimePicker from input HTML element
  $("#datetimepicker").kendoDateTimePicker({
    value: new Date(),
    dateInput: true
  });

  $(".k-nav-prev").click(function() {
    alert("previous month button clicked");
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
    <base href="https://demos.telerik.com/kendo-ui/datetimepicker/index">
    <style>html { font-size: 14px; font-family: Arial, Helvetica, sans-serif; }</style>
    <title></title>
    <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2020.3.1118/styles/kendo.default-v2.min.css" />

    <script src="https://kendo.cdn.telerik.com/2020.3.1118/js/jquery.min.js"></script>
    <script src="https://kendo.cdn.telerik.com/2020.3.1118/js/kendo.all.min.js"></script>
</head>

<body>
        <div id="example">
            <div class="demo-section k-content">
                <h4>Remind me on</h4>
                <input id="datetimepicker" title="datetimepicker" style="width: 100%;" />
            </div>
            <p id="test">
              Hello world
            </p>
        </div>
</body>
</html>

回答1:


The thing is that Kendo is already binding to clicks to those links and intercepting them. You need to use capture to well, capture the event before they do.

The following code will do it, but sometimes it will capture the click on the a.k-nav-prev element, and sometimes on the span with the icon inside:

document.addEventListener('click', function(e) {
  console.log(e);
  if (!e.target.classList.contains("k-nav-prev") && !e.target.parentNode.classList.contains("k-nav-prev")) {
    return;
  }
  console.log("Do your thing here");
}, true);

Demo: https://dojo.telerik.com/@GaloisGirl/etEwOYEp

More on capture here: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener



来源:https://stackoverflow.com/questions/65030733/kendoui-datetime-picker-calling-an-on-click-function-when-previous-next-month

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!