无法弄清楚为什么我的javascript没有做任何事情

时间:2015-04-07 18:04:47

标签: javascript jquery ruby-on-rails ruby ruby-on-rails-4

问题是我有simple_form视图,用户可以选择leave_start和leave_end日期..我想得到这个范围之间的日期以及例子是leave_start = 4/1/15 leave_end = 4/5/15之间的日期将是(4/1/15,4/2/15,4/3/15,4/4 / 15,4 / 5/15)我有一个点击功能但是当我点击gdates文本框时什么也没有出现,我希望日期显示在这个文本字段(4/1 / 15,4 / 2 / 15,4 / 3 / 15,4 / 4 / 15,4 / 5/15)任何帮助将是很大的赞赏。

这是我的app.js

 // JavaScript include manifest for the non-angular pages
 //
 //= require jquery
 //= require jquery_ujs
 //= require jquery.ui.all
 //= require best_in_place
 //= require bootstrap
 //= require jquery.ui.datepicker
 //= require entry



$(document).ready(function() {


    $('#gdates').click(function() {

        var start = $('#leave_start').datepicker("getDate"),
            end = $('#leave_end').datepicker("getDate"),
            currentDate = new Date(start),
            between = []
        ;

        while (currentDate <= end) {
            between.push(new Date(currentDate));
            currentDate.setDate(currentDate.getDate() + 1);
        }
        $('#gdate').val();
    });
  });

这是我的entry.js,它有datepicker

 jQuery ->

   $('#leave_start').datepicker({minDate: 0, beforeShowDay: $.datepicker.noWeekends });


   $('#leave_end').datepicker({minDate: 0, beforeShowDay: $.datepicker.noWeekends });

我的观点

  =simple_form_for @entry, :url => url_for(:controller => 'entry', :action => 'create'), :method => :post do |f|

    %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;'}
      %th.lt Leave End:
      %td.lt= f.text_field :leave_end,  :label => false, :id => 'leave_end', :input_html => {:value => ''}
      %td.lt= f.error :leave_end, :class => 'er'


    %table.table.table-bordered.table-striped{:style => 'table-layout:fixed; width:100% !important;'}
      %th.lt Dates:
      %td.lt= f.text_field :all_dates, :label => false, :id => 'gdates', :input_html => {:value => ''}

    %table.table.table-bordered.table-striped{:style => 'table-layout:fixed; width:100% !important;'}
    = f.button :submit, "Submit", :class => 'btn btn-primary', :style => 'margin-left:50px;'

在我的参赛作品模型中,我有一种方法可以获得leave_start和leave_end之间的天数...我不认为这会导致问题..

参赛作品型号

   def range_days
     self.range_days = only_weekdays(leave_start.to_date..(leave_end.to_date)).to_i
   end

   private

   #works with l_s to l_e to calc amount of days minus weekends...
   def only_weekdays(range)
     range.select { |d| (1..5).include?(d.wday) }.size
   end
 end

1 个答案:

答案 0 :(得分:0)

添加评论作为答案:

你得到什么输出?这是不正确currentDate.setDate(currentDate.getDate() + 1);,因为它可能会将日期增加一秒,获得中间日期并不容易,需要增加一天并检查二月,月末,闰年等。< / p>

您也不会向$('#gdate').val();添加任何内容,请尝试$('#gdate').val(JSON.stringify(between));

更新:

如果您想以其他格式在文本框中显示日期,请执行以下操作:

var formated_dates = between.reduce(function(dates, date){
    dates.push(date.toString());
    return dates;
}, []);
$('#gdate').val(formated_dates.join("\n"));