【问题标题】:datepicker value is blank when disabled, jquery禁用时,日期选择器值为空白,jquery
【发布时间】:2010-06-08 22:08:34
【问题描述】:

我对 jQuery 还很陌生。我在用户控件中有一个 Jquery 日期选择器。我在日期选择器中添加了一个“禁用”属性。每当我保存页面(具有此用户控件)时,禁用设置为 true 的日期选择器为空。所有其他日期选择器都可以正常保存。

这是我的代码。

ASPX

ASCX

ASCX 代码隐藏

Public Property Disable() As Boolean
        Get
           Return (txtDate.Disabled = True)
        End Get
        Set(ByVal bValue As Boolean)

            If (bValue = True) Then
                txtDate.Attributes.Add("Disabled", "True")
            Else
                txtDate.Attributes.Remove("Disabled")
            End If

        End Set


    End Property

我的 Jquery

$(document).ready(function() {

    $("input[id$=txtDate]").datepicker({ showOn: 'button', buttonImage: '<%=ConfigurationSettings.AppSettings("BASE_DIRECTORY")%>/Images/el-calendar.gif', buttonImageOnly: true });
    $("input[id$=txtDate]").mask("99/99/9999", { placeholder: " " });


    //Disable datepicker if "disable=true"
    $("input[id$=txtDate]").each(function() {

    if ($("input[id$=" + this.id + "]").attr("Disabled") == "True") {          
            $("input[id$=" + this.id + "]").datepicker("disable");               
        }
        else if ($("input[id$=" + this.id + "]").attr("Disabled") == "False") {
            $("input[id$=" + this.id + "]").datepicker("enable");
        }

    });


});

对不起,我不知道如何格式化这里的代码。我为混乱的代码道歉。 谁能告诉我为什么 datepicker 值在禁用时为空,但在其他情况下工作正常? 提前谢谢。

【问题讨论】:

  • 这是一个 HTML 的东西——禁用的表单字段不会被浏览器发送到服务器;改为使用只读。

标签: jquery datepicker


【解决方案1】:

只是猜测,但请尝试使用 READONLY 而不是 DISABLED?

【讨论】:

    【解决方案2】:

    我认为也使用只读属性为true,不会改变回发中的初始值

    如果坚持通过只读属性使用它,您将更改 get Text 的覆盖 像这样

    public override string Text
    {
        get
        {
            if (!base.ReadOnly)
                return base.Text;
    
            string s = HttpContext.Current.Request.Params[this.UniqueID];
            if (s == null)
                return base.Text;
            else
            {
                base.Text = s;
                return s;
            }
        }
        set
        {
            base.Text = value;
        }
    }
    

    问候

    【讨论】:

      【解决方案3】:

      这段代码对我有用

      $('input:checkbox[name=cmpltInd]').change(function () {
         if ($(this).is(':checked')) {
            $('#efctDt').attr('readonly', true).datepicker("option", "showOn", "off");
         }
         else {
            $('#efctDt').attr('readonly', false).datepicker("option", "showOn", "focus");
         }
      });
      if ( ($('input:checkbox[name=cmpltInd]') != "undefined") && ($('input:checkbox[name=cmpltInd]').prop("checked")== true)) {
           $('#efctDt').attr('readonly', true).datepicker("option", "showOn", "off");
       }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-04
        • 1970-01-01
        • 1970-01-01
        • 2018-05-28
        • 1970-01-01
        相关资源
        最近更新 更多