Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何动态/响应地更改jQuery datepicker月数_Javascript_Jquery_Dynamic_Responsive Design_Datepicker - Fatal编程技术网

Javascript 如何动态/响应地更改jQuery datepicker月数

Javascript 如何动态/响应地更改jQuery datepicker月数,javascript,jquery,dynamic,responsive-design,datepicker,Javascript,Jquery,Dynamic,Responsive Design,Datepicker,我问这个问题是因为我在另一个问题上找不到答案。如果有,请联系我 我有一个jQuery日期选择器,我在上面设置参数numberOfMonths:2 我希望它是1,如果屏幕大小低于某个数字(如768px)。我试过: $(window).resize(function(){ if($(window).width() < 768){ $('#datepicker').datepicker({ numberOfMonths: 1 })

我问这个问题是因为我在另一个问题上找不到答案。如果有,请联系我

我有一个jQuery日期选择器,我在上面设置参数numberOfMonths:2

我希望它是1,如果屏幕大小低于某个数字(如768px)。我试过:

$(window).resize(function(){
    if($(window).width() < 768){
        $('#datepicker').datepicker({
            numberOfMonths: 1
        })
    }
}
$(窗口)。调整大小(函数(){
如果($(窗口).width()<768){
$(“#日期选择器”)。日期选择器({
月数:1
})
}
}
但由于日期选择器已经初始化,因此无法工作

我能做什么?

如果日期选择器已初始化,则应使用选项设置器:

$( "#datepicker" ).datepicker( "option", "numberOfMonths", [ 2, 3 ] );

参数也可以是一个数字,就像您初始化它的方式一样。请查看链接以了解更多信息。

您的思路是正确的,但是有足够多的东西可以证明是有用的,因此我想发布完整的答案

首先,

以下是我提出的代码,并附有解释每个元素的注释:

// "No-conflict" jQuery document ready, to ensure doesn't collide with other libraries
jQuery(function($) {
  // The initial datepicker load
  $('#datepicker').datepicker({
    numberOfMonths: 3
  });

  // We're going to "debounce" the resize, to prevent the datePicker
  // from being called a thousand times.  This will help performance
  // and ensure the datepicker change is only called once (ideally)
  // when the resize is OVER
  var debounce;
  // Your window resize function
  $(window).resize(function() {
    // Clear the last timeout we set up.
    clearTimeout(debounce);
    // Your if statement
    if ($(window).width() < 768) {
      // Assign the debounce to a small timeout to "wait" until resize is over
      debounce = setTimeout(function() {
        // Here we're calling with the number of months you want - 1
        debounceDatepicker(1);
      }, 250);
    // Presumably you want it to go BACK to 2 or 3 months if big enough
    // So set up an "else" condition
    } else {
      debounce = setTimeout(function() {
        // Here we're calling with the number of months you want - 3?
        debounceDatepicker(3)
      }, 250);
    }
  // To be sure it's the right size on load, chain a "trigger" to the
  // window resize to cause the above code to run onload
  }).trigger('resize');

  // our function we call to resize the datepicker
  function debounceDatepicker(no) {
    $("#datepicker").datepicker("option", "numberOfMonths", no);
  }

});
/“无冲突”jQuery文档准备就绪,以确保不会与其他库冲突
jQuery(函数($){
//初始日期选择器加载
$(“#日期选择器”)。日期选择器({
月数:3
});
//我们将“取消”调整大小,以防止日期选择器
//这将有助于提高性能
//并确保datepicker更改只调用一次(理想情况下)
//当调整大小结束时
var去盎司;
//窗口大小调整功能
$(窗口)。调整大小(函数(){
//清除上次设置的超时。
清除超时(去盎司);
//你的if语句
如果($(窗口).width()<768){
//将去抖动指定为一个小的超时,以“等待”直到调整大小结束
解盎司=设置超时(函数(){
//我们打电话来告诉你你想要的月数-1
去BounceDatePicker(1);
}, 250);
//如果足够大的话,你大概想让它恢复到2到3个月
//所以设置一个“else”条件
}否则{
解盎司=设置超时(函数(){
//我们打电话告诉你你想要的月数-3?
去BounceDatePicker(3)
}, 250);
}
//为确保装载时尺寸正确,请将“触发器”链接到
//调整窗口大小以使上述代码在加载时运行
}).trigger('resize');
//我们调用函数来调整日期选择器的大小
函数去BounceDatePicker(否){
$(“#日期选择器”)。日期选择器(“选项”,“numberOfMonths”,no);
}
});

如果您不熟悉“去Bounce”的概念,该概念是防止代码在事件的每个实例上运行。在这种情况下,500像素的大小调整可能会触发数百次“resize”事件,如果我们没有“去Bounce”,datepicker函数将被调用数百次。显然,我们并不关心所有的“临时”调用datepicker时,我们实际上只关心最后一个,它是由上次调整大小事件触发的,它应该反映用户停止时的最终大小。

它对datepicker工作正常。但是我使用
multiDatesPicker
。我收到以下错误
jquery-3.3.1.min.js:2未捕获错误:方法选项在上不存在jQuery.multiDatesPicker
我的代码是
$(“#日期选择器”)。multiDatesPicker(“选项”,{numberOfMonths:no});
请回答这个问题