Apply jQuery datepicker to multiple instances

后端 未结 12 2237
时光说笑
时光说笑 2020-11-27 14:04

I\'ve got a jQuery date picker control that works fine for once instance, but I\'m not sure how to get it to work for multiple instances.



        
相关标签:
12条回答
  • 2020-11-27 14:31

    I had a similar problem with dynamically adding datepicker classes. The solution I found was to comment out line 46 of datepicker.js

    // this.element.on('click', $.proxy(this.show, this));
    
    0 讨论(0)
  • 2020-11-27 14:32

    I just had the same problem.

    The correct way to use date pick is $('.my_class').datepicker(); but you need to make sure you don't assign the same ID to multiple datepickers.

    0 讨论(0)
  • 2020-11-27 14:34

    To change use of class instead of ID

    <script type="text/javascript"> 
        $(function() { 
            $('.my_date1').datepicker(); 
            $('.my_date2').datepicker(); 
            $('.my_date3').datepicker(); 
            ... 
        }); 
    </script>
    
    0 讨论(0)
  • 2020-11-27 14:34

    In my case, I had not given my <input> elements any ID, and was using a class to apply the datepicker as in SeanJA's answer, but the datepicker was only being applied to the first one. I discovered that JQuery was automatically adding an ID and it was the same one in all of the elements, which explained why only the first was getting datepickered.

    0 讨论(0)
  • 2020-11-27 14:42

    html:

    <input type="text" class="datepick" id="date_1" />
    <input type="text" class="datepick" id="date_2" />
    <input type="text" class="datepick" id="date_3" />
    

    script:

    $('.datepick').each(function(){
        $(this).datepicker();
    });
    

    (pseudo coded up a bit to keep it simpler)

    0 讨论(0)
  • 2020-11-27 14:42

    A little note to the SeanJA answer.

    Interestingly, if you use KnockoutJS and jQuery together the following inputs with different IDs, but with the same data-bind observable:

    <data-bind="value: first_dt" id="date_1" class="datepick" />
    <data-bind="value: first_dt" id="date_2" class="datepick" />
    

    will bind one (the same) datepicker to both of the inputs (even though they have different ids or names).

    Use separate observables in your ViewModel to bind a separate datepicker to each input:

    <data-bind="value: first_dt" id="date_1" class="datepick" />
    <data-bind="value: second_dt" id="date_2" class="datepick" />
    

    Initialization:

    $('.datepick').each(function(){
        $(this).datepicker();
    });
    
    0 讨论(0)
提交回复
热议问题