Datepicker click event not firing

本小妞迷上赌 提交于 2019-12-12 06:25:31

问题


A quick question that I can't wrap my head around.

We have a simple Datepicker (jQuery UI) with a click event on the td and nothing else. But no matter what I try the click event will not fire.

Example: https://jsfiddle.net/s63y1w6h/

The click event is delaged, since the html changes each time a td is clicked, but the same "nothing" happens again.

As to why I need to click the a, I need the td data attributes, text and its offeset, which will be harder to get from the onSelect method.

Any idea how to fire up the click event?


回答1:


You can still use onSelect to fire your logic when a day is clicked

$('.datepicker').datepicker({
  onSelect:function(dt,obj){
    var day = parseInt(dt.substring(3,5));
    var td;
    $("td>a").each(function(index,elm){
     if(parseInt($(elm).html()) === day){
       td = $(elm).parent();
       return;
      }
    })
    console.log(td)
    alert("You're welcome!!!")
  }
});

Wrote some hack logic to get the td based on the day selected

Working plnkr




回答2:


Try to put your code inside

$(document).ready(function(){
    //Your code here
});

JSFiddle: https://jsfiddle.net/s63y1w6h/1/

EDIT: Try to put your code inside

$(document).ready(function(){
    $('.datepicker').datepicker({
        onSelect: function(){
            //your code here
        }  
    });
});

https://jsfiddle.net/s63y1w6h/2



来源:https://stackoverflow.com/questions/44690700/datepicker-click-event-not-firing

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