在 Ajax CalendarExtender 上选择日期后设置 TextBox.Text

发布于 2024-11-14 02:12:42 字数 982 浏览 3 评论 0原文

在 ASP.NET 页面上,我在页面上有一对 CalendarExtender(ASP.NET 4.0 的 AJAX 控制工具包)控件,充当日期范围。我想要做的是,在用户选择 TextCheckInDate 的值后,如果 TextCheckOutDate 则使用 TextCheckInDate+ 1 填充 TextCheckOutDate代码> 为空。

遗憾的是,我的 jQuery 技能还没有达到我想要的水平。我知道我必须创建一个在 TextCheckInDate 更改时触发的 jQuery 函数,我需要能够读取两个文本框,执行基本的日期算术并写入第二个文本框。实施让我困惑。感谢 这篇文章,我知道使用 date.js 用于我的日期算术,包括在下面......

到目前为止我所拥有的:

$("input[id$='TextCheckInDate']").keyup
    (function (e) {
        checkCheckOutDate( $("#TextCheckInDate").val() );
    }
    );


function checkCheckOutDate(checkInDate) {
    var startDate = Date.parse(checkInDate);
    if ($("#TextCheckOutDate").val() == "") {
        $("#TextCheckOutDate").val((1).days().after(startDate));
    }
}

On an ASP.NET page, I have a pair of CalendarExtender (AJAX Control Toolkit for ASP.NET 4.0) controls on a page acting as a date range. What I want to do is, after the user has selected the value for TextCheckInDate , populate TextCheckOutDate with TextCheckInDate+ 1 if TextCheckOutDate is empty.

Regrettably, my jQuery skills aren't yet where I'd like them to be. I know that I have to create a jQuery function that's fired when TextCheckInDate changes, I need to be able to read both textboxes, perform basic date arithmetic and write to the second textbox. The implementation eludes me. Thanks to this post, I know to use date.js for my date arithmetic, included below...

What I have so far:

$("input[id$='TextCheckInDate']").keyup
    (function (e) {
        checkCheckOutDate( $("#TextCheckInDate").val() );
    }
    );


function checkCheckOutDate(checkInDate) {
    var startDate = Date.parse(checkInDate);
    if ($("#TextCheckOutDate").val() == "") {
        $("#TextCheckOutDate").val((1).days().after(startDate));
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

强者自强 2024-11-21 02:12:42

您可以禁止在日期文本框中输入文本吗?如果是这样,您可以使用以下方法:

<script type="text/javascript">
    function checkInDateChanged(sender, args) {
        var checkInDate = sender.get_selectedDate();

        var checkOutDateExtender = $find("CheckOutdateExtender");
        var checkOutdate = checkOutDateExtender.get_selectedDate();
        if (checkOutdate == null || checkOutdate < checkInDate) {
            checkOutdate = new Date(checkInDate.setDate(checkInDate.getDate() + 1));
            checkOutDateExtender.set_selectedDate(checkOutdate);
        }
    }
</script>
<asp:TextBox runat="server" ID="TextCheckInDate" />
<ajax:CalendarExtender runat="server" ID="CheckInDateCalendarExtender" TargetControlID="TextCheckInDate"
    OnClientDateSelectionChanged="checkInDateChanged" />

<asp:TextBox runat="server" ID="TextCheckOutDate" />
<ajax:CalendarExtender runat="server" ID="CheckOutDateCalendarExtender" BehaviorID="CheckOutdateExtender"
    TargetControlID="TextCheckOutDate" />

在 PreRender 方法中添加以下代码:

TextCheckInDate.Attributes.Add("readOnly", "readonly");
TextCheckOutDate.Attributes.Add("readOnly", "readonly");

Can you prohibit to enter text into date textboxes? If so, you can use following approach:

<script type="text/javascript">
    function checkInDateChanged(sender, args) {
        var checkInDate = sender.get_selectedDate();

        var checkOutDateExtender = $find("CheckOutdateExtender");
        var checkOutdate = checkOutDateExtender.get_selectedDate();
        if (checkOutdate == null || checkOutdate < checkInDate) {
            checkOutdate = new Date(checkInDate.setDate(checkInDate.getDate() + 1));
            checkOutDateExtender.set_selectedDate(checkOutdate);
        }
    }
</script>
<asp:TextBox runat="server" ID="TextCheckInDate" />
<ajax:CalendarExtender runat="server" ID="CheckInDateCalendarExtender" TargetControlID="TextCheckInDate"
    OnClientDateSelectionChanged="checkInDateChanged" />

<asp:TextBox runat="server" ID="TextCheckOutDate" />
<ajax:CalendarExtender runat="server" ID="CheckOutDateCalendarExtender" BehaviorID="CheckOutdateExtender"
    TargetControlID="TextCheckOutDate" />

in PreRender method add code below:

TextCheckInDate.Attributes.Add("readOnly", "readonly");
TextCheckOutDate.Attributes.Add("readOnly", "readonly");
甜`诱少女 2024-11-21 02:12:42

您应该能够稍微调整代码,以便在签入文本框的 change 事件触发时也触发 checkCheckOutDate() 函数。

$("input[id$='TextCheckInDate']").bind('keyup,change', function (e) {
    checkCheckOutDate( $("#TextCheckInDate").val() );
});


function checkCheckOutDate(checkInDate) {
    var startDate = Date.parse(checkInDate);
    if ($("#TextCheckOutDate").val() == "") {
        $("#TextCheckOutDate").val((1).days().after(startDate));
    }
}

请注意,jQuery 中的许多事件方法(例如 click()keyup() 等)只是 bind('click', function() { ... })

You should just be able to tweak your code slightly to also fire your checkCheckOutDate() function when the checkin textbox's change event fires.

$("input[id$='TextCheckInDate']").bind('keyup,change', function (e) {
    checkCheckOutDate( $("#TextCheckInDate").val() );
});


function checkCheckOutDate(checkInDate) {
    var startDate = Date.parse(checkInDate);
    if ($("#TextCheckOutDate").val() == "") {
        $("#TextCheckOutDate").val((1).days().after(startDate));
    }
}

Note that lots of the event methods in jQuery like click(), keyup(), etc are just wrappers around bind('click', function() { ... }).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文