DatePicker in jquery is not loaded for the second time

点点圈 提交于 2020-02-25 11:01:52

问题


I have a datepicker in my page and also I have two radio buttons as follows.

<input type="radio" name="radio1" value="radio1" checked> Radio 1 

<input type="radio" name="radio2" value="radio2" >Radio 2

My datepicker html is:

<input 
       type="text" 
       id="mydatepicker" 
       aria-controls="sample_1" 
       class="form-control input-small input-inline" >

My JS code is:

$('#mydatepicker').datepicker({
        format: 'dd/mm/yyyy',
        beforeShowDay: EnableDisableDates
});

function EnableDisableDates(date) {
    var formattedDate = $.datepicker.formatDate('dd/mm/yy', date);
    if ($("#radio1").is(':checked') == false) {
        if (date < Date.parse('11/23/2015'))
            return {enabled: false};
    }
    else {
         return [true];
    }
}

I want to show all the dates on selecting Radio 1 and I want to disable dates on selecting radio button 2 (Radio 2) before 23-Nov-2015().

The above is my scenario.

My problem is:

For first time it loads, it is working fine for the Radio 1 i.e. it enables all the dates for Radio 1.

On selecting the Radio 2, it is not calling the method EnableDisableDates method of beforeShowDay property in datepicker.

I don't know why it is not calling the method. Help me to get out of this problem.


回答1:


you ca use minDate to disable dates instead of looing it manually which will cost perfomance:

Try this:

JS

$(function() {   
   $(".radio1").change(function () {    
    if($(".radio1:checked").val()=="radio2"){
      $('#mydatepicker').datepicker('destroy');
        $('#mydatepicker').datepicker({
          format: 'dd/mm/yyyy',        
          minDate: '11/23/2015'
        });
    }else{
      $('#mydatepicker').datepicker('destroy');
      $('#mydatepicker').datepicker({
          format: 'dd/mm/yyyy',                  
        });      
   }
  });
 $('#mydatepicker').datepicker({format: 'dd/mm/yyyy'}); 
  });

HTML:

 <input type="radio" class="radio1" name="radio1" value="radio1" checked> Radio 1 
 <input type="radio" class="radio1" name="radio1" value="radio2" >Radio 2<input 
         type="text" 
         id="mydatepicker" 
         aria-controls="sample_1" 
         class="form-control input-small input-inline" >

Fiddle

update:

we have used the destroy to create new instance of datepicker jQuery DatePicker -- Changing minDate and maxDate on the fly see this




回答2:


I faced similar issue. In my case it was a dropdown and based on the dropdown it I was disabling certain days in the datepicker. It was working for the change I made. But for second change it was not working.

Finally the solution I got was datepicker('remove').

So this should work with just adding one line of code in the change function.

$('#mydatepicker').datepicker('remove');

So your code should like this:

$(".radio1").change(function () {
$('#mydatepicker').datepicker('remove');
if($(".radio1:checked").val()=="radio2"){
  $('#mydatepicker').datepicker('destroy');
    $('#mydatepicker').datepicker({
      format: 'dd/mm/yyyy',        
      minDate: '11/23/2015'
    });
}else{
  $('#mydatepicker').datepicker('destroy');
  $('#mydatepicker').datepicker({
      format: 'dd/mm/yyyy',                  
    });      

} });



来源:https://stackoverflow.com/questions/34126840/datepicker-in-jquery-is-not-loaded-for-the-second-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!