我有一个j查询日期选择器,在第一篇文章发布后不起作用。它仅在回发之前有效,但日期选择器在回发之后无效。显示日期选择器的文本框由ajax更新面板包裹。这是我的j查询:

<script type="text/javascript">
    $().ready(function () {
        $('.date-picker').mousedown(function () {

            $('.date-picker').datepicker({
                changeMonth: true,
                changeYear: true,
                showButtonPanel: true,
                dateFormat: 'MM yy',
                onClose: function (dateText, inst) {
                    var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
                    var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
                    $(this).datepicker('setDate', new Date(year, month, 1));
                }
            });
        });
    });
</script>
<style type="text/css">
.ui-datepicker-calendar {
    display: none;
    }
</style>

最佳答案

您的问题是在部分回发中,这是当UpdatePanel引起回发时发生的,没有重新加载DOM,因此您的$().ready(function () {将不会触发并连接日期选择器。这是那些ASP.NET AJAX陷阱之一。

解决此问题的一种方法是,当PageRequestManager通过UpdatePanel进行部分回发时调用JavaScript函数,如下所示:

<script type="text/javascript">
    $().ready(function () {
        $('.date-picker').mousedown(function () {

            $('.date-picker').datepicker({
                changeMonth: true,
                changeYear: true,
                showButtonPanel: true,
                dateFormat: 'MM yy',
                onClose: function (dateText, inst) {
                    var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
                    var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
                    $(this).datepicker('setDate', new Date(year, month, 1));
                }
            });
        });
    });

    var prm = Sys.WebForms.PageRequestManager.getInstance();

    prm.add_endRequest(function() {
        // Re-bind your jQuery objects here

    });
</script>



注意:这是jQuery和ASP.NET AJAX稍加调整后效果不佳的地方之一。

关于asp.net - jQuery datepicker回发后不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20135778/

10-13 06:19