I want to be able to set a random date for my datepicker

我与影子孤独终老i 提交于 2019-12-13 03:48:11

问题


I have a date picker called leave_start, I would like the ability to set a min date that isn't static so a person could choose dates after the day they selected. How could I go about doing something like this and for some reason when I click on the future select box it doesn't reload date picker how could I fix that as well?

So when they select either yes or no for future select box. It will reformat the date picker accordingly.

So this is my view where the sick_day input would be the sick day text field would be the min date they would set.

=simple_form_for @entry, :url => url_for(:controller => 'entry', :action 

%td.lt= f.error :range_days, :class => 'er'

%table.table.table-bordered.table-striped{:style => 'table-layout:fixed; width:100% !important;'}

  %td.lt= f.text_field :sick_day, :id => 'sick_day', :placeholder => 'Optional Comment', :input_html => {:value => ''}
  %td.lt= f.error :indirect_id, :class => 'er'

%table.table.table-bordered.table-striped{:style => 'table-layout:fixed; width:100% !important;'}
  %th.lt
  %td.lt= f.input_field :future, :as => :select, :id => 'future', :label => false, :collection => ["YES", "NO"]
  %td.lt= f.error :future, :class => 'er'

%table.table.table-bordered.table-striped{:style => 'table-layout:fixed; width:100% !important;'}
  %th.lt Leave Start:
  %td.lt= f.text_field :leave_start,  :label => false, :id => 'leave_start', :input_html => {:value => ''}
  %td.lt= f.error :leave_start, :class => 'er'


%table.table.table-bordered.table-striped{:style => 'table-layout:fixed; width:100% !important;'}
= f.button :submit, "Submit", :class => 'customSub'

And here is my JavaScript

 $(document).ready(function(){
    $('#future').click(function() {
       var selected = $(this).val();
       var new_day = $('#sick_day').val();
       if (selected == 'YES') {
          $('#leave_start').datepicker({minDate: new_day });
       }
       if (selected == 'NO') {
          $('#leave_start').datepicker({minDate: 0, beforeShowDay: $.datepicker.noWeekends });
       } else {
       }
    });
  });

I tried this but got an error (TypeError: "#sick_day".val is not a function)

fixed this forgot the $ in front of var new_day = ('#sick_day').val();


回答1:


After datepicker has been initialised the method to change the options is different see API

Need to add "option" before the changed value pair/array.

Try this:

$(document).ready(function(){
    //create datepicker
    $('#leave_start').datepicker();

    $('#future').click(function() {
       var selected = $(this).val();
       var new_day = $('#sick_day').val();
       if (selected == 'YES') {
          $('#leave_start').datepicker("option", "minDate", new_day );
       } else if (selected == 'NO') {
          $('#leave_start').datepicker("option", {minDate: 0, beforeShowDay: $.datepicker.noWeekends });
       }
    });
});


来源:https://stackoverflow.com/questions/29850690/i-want-to-be-able-to-set-a-random-date-for-my-datepicker

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