简体   繁体   English

如何使用javascript在单个页面中添加多个日历

[英]how to add more than one calender in a single page using javascript

I am trying to add two calenders in a single page using javascript. 我正在尝试使用javascript在单个页面中添加两个日历。

But it is not working for me for the second field. 但这对我来说对第二个领域不起作用。 I tried the same for jQuery also. 我也尝试过相同的jQuery。 Still facing the same problem. 仍然面临着同样的问题。

Can any one helpme . 谁能帮我 。 Thanks in advance. 提前致谢。

Here is the code: 这是代码:

$(".datepicker").each(function () {
    $(this).datepicker({
        dateFormat: 'dd-mm-yy',
        changeMonth: true,
        changeYear: true,

    });
});

You don't need to do this in loop (although that should also work.) : 您不需要循环执行此操作(尽管这也可以工作。)

$(".datepicker").datepicker({
  // your stuff here
});

Because class selectors returns a collection so this would work. 因为类选择器返回一个集合,所以这将起作用。

Sample Demo 样本演示


What could be the issues: 可能是什么问题:

What seems to me is most probably you have same ids to both the elements but still in this case your datepicker for both inputs will be intialized but on selection of a date only first field will be filled with dates if same id applied on every datepicker input. 在我看来,很可能两个元素都具有相同的ID,但是在这种情况下,两种输入的日期选择器都会初始化,但是在选择日期时,如果对每个日期选择器输入都应用相同的ID,则只有第一个字段会填充日期。

Sample Demo with same ids. 具有相同ID的示例演示。

Why not just this: 为什么不这样:

$(".datepicker").datepicker({ 
    dateFormat: 'dd-mm-yy', 
    changeMonth: true, 
    changeYear: true
  });

It should apply plugin for each control with 'datepicker' class 它应该为带有“ datepicker”类的每个控件应用插件

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

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