【问题标题】:Aspx datepicker browser compatibilityAspx 日期选择器浏览器兼容性
【发布时间】:2017-10-20 18:18:16
【问题描述】:

我是使用 aspx 项目的新手,在前端我有这样的表格:

<table style="width: 450px" id="tabla1">
  <tr>
    <th style="text-align:left">
      <asp:Label runat="server" ClientIDMode="Static" ID="label_fechaini" Text="Fecha de Apertura:" Width="50%"></asp:Label>
      <br />
    </th>
    <th style="text-align:left">
      <asp:Label runat="server" ClientIDMode="Static" ID="label_fechafin" Text="Fecha de Cierre:" Width="50%"></asp:Label>
      <br />
    </th>
  </tr>
  <tr>
    <td>
      <asp:TextBox type="date" ClientIDMode="Static" runat="server" ID="fecha_ini"></asp:TextBox>
    </td>
    <td>
      <asp:TextBox type="date" ClientIDMode="Static" runat="server" ID="fecha_fin"></asp:TextBox>
    </td>
  </tr>
</table>

在 chrome 浏览器中,我可以看到日期选择器没有这样的问题:

问题是与其他浏览器的兼容性,例如 IE 不显示 datepicker。我搜索到 CanIuse web。并且 IE 不支持日期。我该怎么做才能解决这个问题?

IE图片:

【问题讨论】:

    标签: asp.net datepicker


    【解决方案1】:

    我假设您没有运行 IE 7 或任何真正旧的东西。 我会调查JQueryUI Datepicker。 它适用于最近的 IE 和 Firefox/Chrome/Mozilla 等。

    $("#fecha_ini").datepicker();
    

    您可能必须使用此引用输入 ID。

    $("#<%= fecha_ini.ClientID %>").datepicker();
    

    编辑

    我的错。我相信您不能将 datepicker 应用于 asp:TextBox 控件,并且它仅适用于 html 输入。我从答案中删除了那部分。

    您需要包含 JQUERY 和 JQUERYUI 才能完成这项工作。

    <script>
        $(document).ready(function ()
        {
            // Regular way to apply datepicker
            $("#fecha_ini").datepicker();
            $("#fecha_fin").datepicker();
    
            // If you get error because it cannot find the control use this.
            //$("#<%= fecha_ini.ClientID %>").datepicker();
            //$("#<%= fecha_fin.ClientID %>").datepicker();
        });
    </script>
    <table style="width: 450px" id="tabla1">
        <tr>
            <th style="text-align:left">
                <asp:Label runat="server" ClientIDMode="Static" ID="label_fechaini" Text="Fecha de Apertura:" Width="50%"></asp:Label>
                <br />
            </th>
            <th style="text-align:left">
                <asp:Label runat="server" ClientIDMode="Static" ID="label_fechafin" Text="Fecha de Cierre:" Width="50%"></asp:Label>
                <br />
            </th>
        </tr>
        <tr>
            <td>
                <asp:TextBox type="date" ClientIDMode="Static" runat="server" ID="fecha_ini"></asp:TextBox>
            </td>
            <td>
                <asp:TextBox type="date" ClientIDMode="Static" runat="server" ID="fecha_fin"></asp:TextBox>
            </td>
        </tr>
    </table>
    

    【讨论】:

    • 我应该改用&lt;asp:TextBox type="date" ClientIDMode="Static" runat="server" ID="fecha_fin"&gt;&lt;/asp:TextBox&gt;这一行&lt;script&gt; $(function () { $("#&lt;%= fecha_ini.ClientID %&gt;").datepicker(); }); &lt;/script&gt;?
    • @Pepe 不,此 JavaScript 将日期选择器功能添加到现有文本框。它不是文本框本身的替代品。您仍然需要标记中的文本框声明。
    • 我的文本框应该是什么样的。它需要type= date?还是简单的&lt;input type="text" ID=fecha_fin&gt;?
    • @Pepe 请回顾一下答案,我对其进行了编辑以消除对“' 的混淆。您可以保持工作原样。只需确保包含 jquery/jqueryUI CDN 并使用脚本即可。