简体   繁体   English

jQuery Datepicker-在页面加载时填充输入字段

[英]jquery Datepicker - fill input fields on page load

I have the following three input boxes, in an ASP Classic page, each associated with a corresponding jquery datepicker calendar: 我在ASP Classic页面中有以下三个输入框,每个输入框都与相应的jquery datepicker日历相关联:

<br />
Start Date: <input type="text" id="dtpDateStart" name="dtpDateStart" />
<br />
<br />
End Date:   <input type="text" id="dtpDateEnd" name="dtpDateEnd" />
<br />
<br />
Return By:  <input type="text" id="dtpDateReturn" name="dtpDateReturn" />
<br />

And the JQuery script: 和JQuery脚本:

<script type="text/javascript">
$(function () {
    $("#dtpDateStart").datepicker({
        beforeShowDay: $.datepicker.noWeekends,
        changeMonth: true,
        changeYear: true,
        dateFormat: 'm/d/yy',
        onClose: function (selectedDate) {
            $("#dtpDateEnd").datepicker("option", "minDate", selectedDate);
        }
    });
    $("#dtpDateEnd").datepicker({
        beforeShowDay: $.datepicker.noWeekends,
        changeMonth: true,
        changeYear: true,
        dateFormat: 'm/d/yy',
        onClose: function (selectedDate) {
            $("#dtpDateStart").datepicker("option", "maxDate", selectedDate);
            $("#dtpDateReturn").datepicker("option", "minDate", selectedDate);
        }
    });
    $("#dtpDateReturn").datepicker({
        beforeShowDay: $.datepicker.noWeekends,
        changeMonth: true,
        changeYear: true,
        dateFormat: 'm/d/yy',
        onClose: function (selectedDate) {
            $("#dtpDateEnd").datepicker("option", "maxDate", selectedDate);
        }
    });

});

When a date is chosen in the Start Date calendar (dtpDateStart), the End Date (dtpDateEnd) calendar will only allow for a date after the start date to display. 在开始日期日历(dtpDateStart)中选择日期后,结束日期(dtpDateEnd)日历将只允许显示开始日期之后的日期。 And so on with the return date calendar (dtpDateReturn). 以此类推,以返回日期日历(dtpDateReturn)为例。

But I now need to add a two more items of functionally: 但是我现在需要在功能上添加另外两项:

  1. On page load, fill todays date into the Start and End input fields and tomorrows date into the ReturnBy input field. 在页面加载时,将今天的日期填写到“开始”和“结束”输入字段中,并将明天的日期填写到“返回依据”输入字段中。

  2. if the StartDate is changed, automatically update the EndDate and ReturnByDate fields to the same date (or +1), if the dates already in them are before the StartDate (otherwise leave them if they are after the StartDate) 如果更改了StartDate,则将EndDate和ReturnByDate字段自动更新为相同的日期(或+1)(如果它们中的日期早于StartDate(如果在StartDate之后,则保留它们)

I have the first item working elsewhere in a simple calendar with the following: 我的第一个项目在简单的日历中的其他地方工作,内容如下:

$("#jqDatePicker").datepicker("setDate", new Date);

but can't seem to roll it into the above function. 但似乎无法将其纳入上述功能。

Any suggestions? 有什么建议么?

Not sure of the etiquette for answering my own question rather than editing it, but here what I ended up doing. 不确定回答我自己的问题而不是编辑问题的礼节,但最终我做了什么。 per @jesus.tesh suggestion, I added the values (ie todays date) to the input fields on page load via server side eg 根据@ jesus.tesh的建议,我通过服务器端将值(即今天的日期)添加到页面加载的输入字段中,例如

<% 
  Dim todaysDate 
  todaysDate = Date()
%>

<input type="text" id="dtpDateEnd" name="dtpDateEnd" value="<%=todaysDate%>" />

I also changed my above javascript/jQuery to work better for my needs: 我还更改了上面的javascript / jQuery,以更好地满足我的需求:

<script type="text/javascript">

$(function () {
    $("#dtpDateReturn").datepicker({ beforeShowDay: $.datepicker.noWeekends, dateFormat: 'm/d/yy', changeMonth: true, changeYear: true });
    $("#dtpDateReturn").datepicker("setDate", +1);
});

$(function () {
    $("#dtpDateStart").datepicker({
        beforeShowDay: $.datepicker.noWeekends,
        changeMonth: true,
        changeYear: true,
        dateFormat: 'm/d/yy',
        onClose: function (selectedDate) {
            $("#dtpDateEnd").datepicker("option", "minDate", selectedDate);
        }
    });
    $("#dtpDateEnd").datepicker({
        beforeShowDay: $.datepicker.noWeekends,
        changeMonth: true,
        changeYear: true,
        dateFormat: 'm/d/yy',
        onClose: function (selectedReturnDate) {
            $("#dtpDateReturn").datepicker("option", "minDate", selectedReturnDate);
        }
    });

    $("#dtpDateReturn").datepicker( {});
});

and it pretty much does what I need 这几乎可以满足我的需求

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM