为什么Jquery datePicker不能在asp.net文本框上工作

时间:2014-06-21 19:16:49

标签: javascript jquery asp.net datepicker

我正在尝试使用jquery-ui-1.10.4中提供的jquery datepicker。但问题是它没有服务器端的html输入标签工作正常。

一旦我将它用于服务器端,jquery函数就不会执行

编码为

<script type="text/javascript">
    $(function () {
        $("#datepicker").datepicker({
            changeMonth: true,
            changeYear: true
        });
    });

和工件是

<input type="text" id="datepicker"/>//Without runat attribute

但是当添加runat属性时,它会停止工作

<input type="text" id="datepicker" runat="server"/>//Without runat attribute

任何人帮助!!!

2 个答案:

答案 0 :(得分:0)

可能是由于母版页。当您使用母版页时,它会在子页面的每个控件中附加前缀“CPH”。尝试使用类访问。

<强>的Javascript

<input type="text" id="datepicker" runat="server" class="datepicker"/>

<强> HTML

$(function () {
    $(".datepicker").datepicker({
        changeMonth: true,
        changeYear: true
    });
});

如果您不想使用类,那么第二种方法是使用$("#CPH_datepicker")作为选择器

答案 1 :(得分:0)

服务器端控件根据母版页内的嵌套层次结构自动生成ID。

所以这是一个选择:

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

或者这个:

$(function () {
    $("#<%= datepicker.ClientId %>").datepicker({
        changeMonth: true,
        changeYear: true
    });
});

第二个选项更可取,因为它是强类型的。虽然使用第一个解决方案,您还可以在页面,web.config或machine.config级别更改ClientIdMode。