Jquery 如何动态设置今日日期

Jquery 如何动态设置今日日期,jquery,ruby-on-rails,Jquery,Ruby On Rails,嗨,我有一个名为“日历类型”的下拉列表。我有四个价值观,比如今天,运营,休假,事件。我想在下拉列表中选择“今天”值时自动显示“今天日期”。我不知道如何做到这一点。如果你有什么想法,请告诉我 这是我的form.html 开始日期 截止日期 这是另一个休假、事件、操作的日期选择器 试试这个: var currentDate = new Date(); $("#fromdate").datepicker("setDate",currentDate); $("#todate").datepicke

嗨,我有一个名为“日历类型”的下拉列表。我有四个价值观,比如今天,运营,休假,事件。我想在下拉列表中选择“今天”值时自动显示“今天日期”。我不知道如何做到这一点。如果你有什么想法,请告诉我

这是我的form.html

开始日期

截止日期

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

试试这个:

 var currentDate = new Date();  
$("#fromdate").datepicker("setDate",currentDate);
$("#todate").datepicker("setDate",currentDate);
已解决问题:

添加一个javascript onchange方法

<% 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%>
<%= 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>
 var currentDate = new Date();  
$("#fromdate").datepicker("setDate",currentDate);
$("#todate").datepicker("setDate",currentDate);
<% 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%>
function changeDate(obj) {
        if(obj.value == "TOD"){
            today = new Date();
            todayFormat = today.getDate() + "/" + (today.getMonth() + 1) + "/" + today.getFullYear().toString();
            document.getElementById("fromdate").value = todayFormat;
            document.getElementById("todate").value = todayFormat;                              
        }
}