![](/img/trans.png)
[英]My innerHTML command isn't changing anything and i can't figure out why
[英]Can't figure out why my javascript isn't doing anything
问题是我有simple_form视图,用户可以选择一个leaves_start和leave_end日期。我想使日期介于此范围之间,例如,leave_start = 4/1/15 leaves_end = 4/5/15以及之间的日期是(4/1 / 15、4 / 2 / 15、4 / 3 / 15、4 / 4 / 15、4 / 5/15),我具有on click功能,但是当我单击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
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
添加评论作为答案:
你得到什么输出? 这是不正确的currentDate.setDate(currentDate.getDate() + 1);
因为它可能使日期增加一秒钟,所以获取中间日期并非易事,您需要将日期增加一天,并检查2月,月末,leap年等。
您也不会在$('#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"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.