Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.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
Jquery 如何访问正在应用datepicker的输入_Jquery_Datepicker_This - Fatal编程技术网

Jquery 如何访问正在应用datepicker的输入

Jquery 如何访问正在应用datepicker的输入,jquery,datepicker,this,Jquery,Datepicker,This,我将jquery datepicker应用于具有classtxt date的输入元素: $('.txt-date').datepicker({ showAnim: "blind", changeMonth: true, changeYear: true, dateFormat: "dd.mm.yy", numberOfMonths: 2 }); 如你所见,我指定显示2个月。但这不是我想要的所有输入字段的行为。为了使它更灵活,我想根据自定义属性(类似于数

我将jquery datepicker应用于具有class
txt date
的输入元素:

 $('.txt-date').datepicker({
    showAnim: "blind",
    changeMonth: true,
    changeYear: true,
    dateFormat: "dd.mm.yy",
    numberOfMonths: 2
});
如你所见,我指定显示2个月。但这不是我想要的所有输入字段的行为。为了使它更灵活,我想根据自定义属性(类似于
数据显示月份
)值来确定
numberOfMonths
属性的值。我试图通过
$(this)
访问输入元素,如下所示

<input type="text" class="txt-date" data-shown-months="2"/>

     $('.txt-date').datepicker({
    showAnim: "blind",
    changeMonth: true,
    changeYear: true,
    dateFormat: "dd.mm.yy",
    numberOfMonths: $(this).data('shown-months')
});

$('.txt date').datepicker({
showAnim:“盲人”,
变化月:对,
变化年:是的,
日期格式:“年月日”,
numberOfMonths:$(此).data('showed-months')
});

但那没用。我也尝试了
$(this).attr('data-show-months')
,以确保jquery
data
函数没有问题。似乎
$(这个)
指的是日期选择器本身。至少它没有引用源输入元素。您知道我将如何访问源输入元素吗?

指的是当您进行
datepicker()
调用(可能是
窗口
)时,此是什么。将
放置在对象参数中不会更改其上下文

改为:

$('.txt-date').datepicker({
  numberOfMonths: $('.txt-date').data('shown-months') //"this" is probably window
});
如果您有多个具有“txt date”类的元素,每个元素都有自己的“Showed months”值,那么您可以在一个循环中初始化它们。在这种情况下,
将指向每个元素:

$('.txt-date').each(function() {
  $(this).datepicker({
    numberOfMonths: $(this).data('shown-months') //"this" is the current element
  });
});

始终指运行代码的所有者/调用者。由于代码直接在页面中运行,因此所有者是
窗口
对象

如果将代码包装在属于
输入的事件中
,则
将引用
输入
,因为它是事件的所有者:

$('.txt date').one(“focusin”,function(){
$(此).datepicker({
showAnim:“盲人”,
变化月:对,
变化年:是的,
日期格式:“年月日”,
numberOfMonths:$(此).data(“显示月份”),
});
});

谢谢,@RIck。真的解决了。从来没有想过
这个
可以引用窗口。