如何动态设置今天的日期

时间:2015-05-21 12:10:22

标签: jquery ruby-on-rails

您好我有一个名为“日历类型”的下拉列表。我有四个值,如“今天,操作,离开,事件”。我想在下拉列表中选择值“今天”时自动显示今天的日期。我不知道如何实现这一目标。如果您有任何想法,请告诉我。

这是我的form.html

<div class="row">
    <div class="col-lg-5">
        <div class="input-group">
            <span class="input-group-addon"><i class="fa fa-envelope"></i> Calender Type</span>
                <% if @calendarlists != nil then%>
                    <%= collection_select(:calendar,:calendar_type, @calendarlists, :parameter, :value,{:prompt=> "Select Calendar Type"},{:class=>"form-control"}) %>
                <%else%>
                    <select id="calendar_calendar_type" name="calendar[calendar_type]" class="form-control">
                        <option value="">Select Calendar Type</option>
                    </select>
                <%end%>
        </div>
    </div>
</div>

开始日期

<%= f.text_field :start_date, :id => 'fromdate',:placeholder => "Start Date", :class => "form-control",:autocomplete => :off,:value => @calendar.start_date.try(:strftime,"%d/%m/%Y"), :readonly => true %>

结束日期

<%= f.text_field :expiration_date, :id => 'todate',:placeholder => "End Date", :class => "form-control",:autocomplete => :off,:value => @calendar.expiration_date.try(:strftime,"%d/%m/%Y"), :readonly => true %>

这是另一个休假,事件,操作的日期选择

<script>
    $("#fromdate").datepicker({
        minDate : new Date(),
        dateFormat : 'dd/mm/yy',
        changeMonth : true,
        changeYear : true,
        numberOfMonths : 1,
        onClose : function(selectedDate) {
            $("#todate").datepicker("option", "minDate", selectedDate);
            this.focus();
        }
    });
    $("#todate").datepicker({
        minDate : new Date(),
        dateFormat : 'dd/mm/yy',
        changeMonth : true,
        changeYear : true,
        numberOfMonths : 1,
        onClose : function(selectedDate) {
            $("#fromdate").datepicker("option", "maxDate", selectedDate);
            this.focus();
        }
    }); 
</script>

enter image description here

2 个答案:

答案 0 :(得分:0)

试试这个:

 var currentDate = new Date();  
$("#fromdate").datepicker("setDate",currentDate);
$("#todate").datepicker("setDate",currentDate);

答案 1 :(得分:0)

解决了问题:

添加javascript onchange方法

ALTER VIEW MyViewA RENAME TO MyViewTmp;
CREATE OR REPLACE VIEW MyViewB AS SELECT col1, col2 FROM MyViewTmp where col3=1;
CREATE MATERIALIZED VIEW MyViewA AS SELECT col1, col2, col3 FROM table;
CREATE OR REPLACE VIEW MyViewB AS SELECT col1, col2 FROM MyViewA where col3=1;
DROP VIEW MyViewTmp;

JS功能

<% if @calendarlists != nil then%>
        <%= collection_select(:calendar,:calendar_type, @calendarlists, :parameter, :value,{:prompt=> "Select Calendar Type"},{:onchange=>'changeDate(this)',:class=>"form-control"}) %>
<%else%>
        <select id="calendar_calendar_type" name="calendar[calendar_type]" class="form-control">
            <option value="">Select Calendar Type</option>
        </select>
<%end%>