0

在这方面需要帮助。我无法在 jquery 中禁用我的日期选择器。我已经进行了研究,但无济于事。下面是不启用/禁用日期选择器的代码。[更新]

    <script type="text/JavaScript">
    function pageLoad() {

        $(function () {
            $('#<%=TextBox_EventStartDate.ClientID %>').datepicker({
                showOn: 'button',
                buttonImage: '../Images/CalendarIcon1.gif',
                changeMonth: true,
                changeYear: true,
                buttonImageOnly: true,
                dateFormat: 'dd/mm/yy'
            });

        });

        $(function () {
            $('#<%=TextBox_EventEndDate.ClientID %>').datepicker({
                showOn: 'button',
                buttonImage: '../Images/CalendarIcon1.gif',
                changeMonth: true,
                changeYear: true,
                buttonImageOnly: true,
                dateFormat: 'dd/mm/yy'
            });
        });

        $("#<%=CheckBox_PayEvent.ClientID %>").click(function () {
            if ($("#<%=CheckBox_PayEvent.ClientID %>").is(":checked")) {
                $("#<%=TextBox_EventStartDate.ClientID %>").attr('readonly', true);
                $("#<%=TextBox_EventStartDate.ClientID %>").datepicker("disable");

                $("#<%=TextBox_EventEndDate.ClientID %>").attr('readonly', true);
                $("#<%=TextBox_EventEndDate.ClientID %>").datepicker("disable");
            }
            else {
                $("#<%= TextBox_EventStartDate.ClientID %>").attr('readonly', false);
                $("#<%= TextBox_EventStartDate.ClientID %>").datepicker("enable");

                $("#<%= TextBox_EventEndDate.ClientID %>").attr('readonly', false);
                $("#<%= TextBox_EventEndDate.ClientID %>").datepicker("enable");
            }
        });
    }

</script>

谢谢您的帮助!

4

6 回答 6

5

2种对我有用的方法:

$( "#datepicker" ).datepicker().datepicker('disable'); //disable [not disabled]
or .datepicker("disable");

没有其他方法适合我:

.datepicker("disabled"); 
.dpSetDisabled(false);
.datepicker( "option", "disabled", true );

我使用 jQuery 1.9

于 2012-10-14T23:52:02.623 回答
2

这个答案有点晚了,但万一这对其他人有帮助,我偶然发现了同样的问题,网上发布的常见解决方案.datepicker("disable")也对我不起作用

我查看了 datePicker 源代码,发现以下内容对我有用。也许正确的答案取决于版本。

禁用控制

$("#<%= TextBox_EventStartDate.ClientID %>").dpSetDisabled(true); 

启用控制

$("#<%= TextBox_EventStartDate.ClientID %>").dpSetDisabled(false); 
于 2012-10-04T01:26:40.520 回答
1

尝试将函数放在禁用逻辑中。例如,如果附加的文本框被禁用,我希望 datepicker 消失,如下所示:

 $(document).ready(function () {
    //Turn off datepicker if the textbox has been disabled.
    if(document.getElementById('<%=txtStartDate.ClientID%>').disabled == true){
        $("#txtStartDate").datepicker('disable');
    }
    else { // Otherwise, enable and set up the .datepicker function 
        $("#txtStartDate").datepicker('enable');

        // .datepicker function goes here
        $('#<%=txtStartDate.ClientID%>').datepicker({
        showOn: "button", 
        buttonImage: "/Admin/Images/calendar.gif", 
        buttonImageOnly: true,
        }); 
    }      
});

我知道帖子很旧,但希望对您有所帮助。

于 2012-10-30T21:57:33.127 回答
0

尝试

$("#<%= TextBox_EventStartDate.ClientID %>").datepicker("disable");
于 2012-08-03T03:59:55.233 回答
0

使用 ({disabled: true}) 肯定是行不通的, datepicker("disable") 就是这样做的。

您确定满足条件语句并触发点击事件吗?你确定代码正在运行吗?

服务器端代码是否可能会回显该空间,以便传递给 JQuery 的 ID 是这样的:“# ElementID”?

于 2012-08-03T04:28:04.783 回答
0

我理解的原因是当您选中/取消选中复选框时,它会回发页面并将日期选择器再次设置为文本框。试试这个

    <script type="text/JavaScript">
    function pageLoad() {

        $(function () {
            $('#<%=TextBox_EventStartDate.ClientID %>').datepicker({
                showOn: 'button',
                buttonImage: '../Images/CalendarIcon1.gif',
                changeMonth: true,
                changeYear: true,
                buttonImageOnly: true,
                dateFormat: 'dd/mm/yy'
            });

        });

        $(function () {
            $('#<%=TextBox_EventEndDate.ClientID %>').datepicker({
                showOn: 'button',
                buttonImage: '../Images/CalendarIcon1.gif',
                changeMonth: true,
                changeYear: true,
                buttonImageOnly: true,
                dateFormat: 'dd/mm/yy'
            });
        });

        $("#<%=CheckBox_PayEvent.ClientID %>").click(function () {
            if ($("#<%=CheckBox_PayEvent.ClientID %>").is(":checked")) {
                $("#<%=TextBox_EventStartDate.ClientID %>").attr('readonly', true);
                $("#<%=TextBox_EventStartDate.ClientID %>").datepicker("disable");

                $("#<%=TextBox_EventEndDate.ClientID %>").attr('readonly', true);
                $("#<%=TextBox_EventEndDate.ClientID %>").datepicker("disable");
            }
            else {
                $("#<%= TextBox_EventStartDate.ClientID %>").attr('readonly', false);
                $("#<%= TextBox_EventStartDate.ClientID %>").datepicker("enable");

                $("#<%= TextBox_EventEndDate.ClientID %>").attr('readonly', false);
                $("#<%= TextBox_EventEndDate.ClientID %>").datepicker("enable");
            }
            return false; //This line will stop the postback to occur
        });
    }

</script>
于 2012-08-03T06:30:17.767 回答