简体   繁体   English

jQuery ui #datepicker调用无限次

[英]jquery ui #datepicker call infinite times

i am developing a events clasification. 我正在开发事件分类。 The date is one of them. 日期是其中之一。 i have jquery-UI datepicker, however this function just calls twice and the function inside on- select just works twice. 我有jquery-UI datepicker,但是此函数仅调用两次,而on-select内的函数仅工作两次。 All the sintax works fine but i need more twice. 所有的sintax都工作正常,但我需要两次以上。 How can i resolve this?` 我该如何解决?

 $(document).on('focus',"#datepicker", function(){ 
          $.datepicker.setDefaults($.datepicker.regional["es"]);
          $(this).removeAttr('datepicker');
           $(this).datepicker({
      firstDay: 1,
      dateFormat: "MM dd, yy",
      monthNames: [ "Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre" ],
      onSelect: function() { 
  var date = $(this).datepicker('getDate');
  var start = new Date(date.getFullYear(), 0, 0);
  var diff = date - start;
  var oneDay = 1000 * 60 * 60 * 24;
  var dayy = Math.floor((diff / oneDay));

$(".fechae").each(function(){
$(".pin").hide();
$(".pin").removeClass('mostar');
var i= parseInt($(this).find("h2").text());
var f = parseInt($(this).find("p").text()); 
var $pin = $(this).parent(".pin");
if ( i<dayy && dayy <f ){
  $pin.addClass("menealo");
}
if ($pin.hasClass("visible")){
  $(".menealo.visible").show();

}else{
  $(".menealo").show();

} 
      });

}}); }}); }); });

The variables are days of year numbers. 变量是一年中的天数。

From what I get is, the datepicker is called multiple times but you need the datepicker creation calls in onfocus because the data is dynamic. 从我得到的是,日期选择器被多次调用,但是由于数据是动态的,因此您需要在onfocus调用日期选择器。

There can be many ways to avoid this - create the data reading part in separate function, or destroy the datepicker every time you bind the new one. 有很多方法可以避免这种情况-在单独的函数中创建数据读取部件,或者在每次绑定新部件时销毁日期选择器。 I'm going with later option here (more because I'm lazy, but I suggest use former solution) - 我将在此处使用稍后的选项(更多原因是我很懒,但是我建议使用以前的解决方案)-

All you need is to add this line of code before date picker creation - 您需要做的就是在创建日期选择器之前添加此行代码-

$(this).datepicker('destroy');
$(this).datepicker({
...
...

This will reset the datepicker on each focus and then recreate it with new data. 这将重置每个焦点上的日期选择器,然后使用新数据重新创建它。

If this is not what you wanted please elaborate and also refine the question. 如果这不是您想要的,请详细说明并完善问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM