我申請jQuery的日期選擇到具有txt-date
類的輸入元素,輸入:我如何可以訪問正被應用於日期選擇器
$('.txt-date').datepicker({
showAnim: "blind",
changeMonth: true,
changeYear: true,
dateFormat: "dd.mm.yy",
numberOfMonths: 2
});
正如你可以看到我指定顯示2個月。但這不是我想要的所有輸入字段的行爲。爲了使它更靈活,我想根據自定義屬性(如data-shown-months
)的值確定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')
});
但是沒有工作。我也試過$(this).attr('data-shown-months')
,以確保它不是jquery data
函數的問題。看來$(this)
是指日期選擇器本身。至少它不涉及源輸入元素。你有什麼想法我將如何訪問源輸入元素?
謝謝,@RIck。它真的把它整理出來了。從來沒有想過'這個'可以指窗口。 –