【问题标题】:Datepicker is not working when adding runat="server"添加 runat="server" 时 Datepicker 不工作
【发布时间】:2017-02-05 20:53:54
【问题描述】:

我指的是这个站点http://jqueryui.com/datepicker/ 的日期选择器,我的编码与给出的源代码完全相同。这些编码工作正常。

但是..

当我在这段代码<input type="text" id="datepicker"> 中添加 runat="server" 时,单击TextBox 时我的日历不会显示。我需要放置 runat="server" 以便我能够在代码后面使用它。

你们的任何建议将不胜感激。谢谢。

【问题讨论】:

标签: jquery asp.net webforms datepicker runatserver


【解决方案1】:

runat=server 添加一个母版页时,它会生成带有母版页的默认样式 ID。所以要避免它,请尝试 ClientIDMode="Static"

<input type="text" id="datepicker" runat="server" ClientIDMode="Static">

没有 ClientIDMode='Static' 生成的 HTML 是

<input type="text" id="MainContent_datepicker" name="ctl00$MainContent$datepicker">

通过使用firebug检查元素

,了解更多信息,请使用firebug for mozilla firefox

【讨论】:

    【解决方案2】:

    我找到了正确的方法。

    用于 ASP.Net 设计

    &lt;asp:TextBox runat="server" id="datepicker"&gt;&lt;/asp:TextBox&gt;

    在客户端服务器端

    $(document).ready(function () { $("[id$=datepicker]").datepicker(); });

    输入$("[id$=datepicker]").datepicker(); 而不是$('#&lt;%= datepicker.ClientID %&gt;').datepicker();,那么日历就会显示出来,并且可以在后面的代码中调用datepicker

    【讨论】:

      【解决方案3】:

      当文本框在视图源中为 runat="server" 时,您可以找到生成了动态前缀的文本框的名称。

      因此,在您的 JavaScript 中,您不应该使用标签中指定的 id 属性,而应该使用 这将给出生成的 ID。

      【讨论】:

        【解决方案4】:

        试试这个

        <script>
         $(document).ready(function () {
         $("body").delegate("#<%=datepicker.ClientID%>", "focusin", function () {
        $(this).datepicker();
         });
         });
          </script> 
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-12-02
          • 2012-09-18
          • 2013-06-24
          • 1970-01-01
          • 1970-01-01
          • 2010-10-28
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多