asp.net 中文本框上的日期选择器不工作
Datepicker on a textbox in asp.net is not working
这是我的日期选择器 JS。
<script type="text/javascript">
$(document).ready(function () {
$(function () {
$("#txtage").datepicker();
});
});
</script>
这是我的 asp 服务器端控件。
<asp:TextBox ID="txtage" runat="server" ReadOnly="true"></asp:TextBox>
我希望当用户到达此文本框时,应打开一个日历,他们可以从中 select 年龄,并且该日期应显示在此文本框中。
请告诉我该怎么做?
你有几个问题。您的 ID 不匹配。您在一个地方使用 txtage
而在另一个地方使用 txtcity
。是复制粘贴错误吗?
如果您使用嵌套模板化控件(或母版页),您在客户端的 ID 可能与服务器端的不同。您可以像这样更改客户端 ID 的生成方式:
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" />
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<script type="text/javascript">
$(document).ready(function () {
$(function () {
$("#txtage").datepicker();
});
});
</script>
<td><asp:TextBox ID="txtage" runat="server" ClientIdMode="static" /></td>
您还没有显示用于确保 jQuery 和 jQuery UI 已加载到页面上的代码。
只需添加 .ClientID
和文本框 ID,如下所示。
JS
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.0/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.11.0/jquery-ui.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$(function () {
$('#<%=txtage.ClientID%>').datepicker();
});
});
我刚刚检查了一下,这个效果很好:
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="testDatePicker.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" />
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<script src="script.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox runat="server" ID="myTextBox"></asp:TextBox>
</div>
</form>
</body>
</html>
script.js
$(function() {
$("#myTextBox").datepicker();
});
我可能会建议您删除 ReadOnly
属性,因为日期选择器会尝试修改文本框,但不太确定。
Asp.Net 需要一个 属性 即 TextMode
加载一个日历,它必须等于 "Date"
- TextMode="Date"
.
示例:
<asp:TextBox ID="txtage" TextMode="Date" runat="server" ReadOnly="true"></asp:TextBox>
这是我的日期选择器 JS。
<script type="text/javascript">
$(document).ready(function () {
$(function () {
$("#txtage").datepicker();
});
});
</script>
这是我的 asp 服务器端控件。
<asp:TextBox ID="txtage" runat="server" ReadOnly="true"></asp:TextBox>
我希望当用户到达此文本框时,应打开一个日历,他们可以从中 select 年龄,并且该日期应显示在此文本框中。
请告诉我该怎么做?
你有几个问题。您的 ID 不匹配。您在一个地方使用 txtage
而在另一个地方使用 txtcity
。是复制粘贴错误吗?
如果您使用嵌套模板化控件(或母版页),您在客户端的 ID 可能与服务器端的不同。您可以像这样更改客户端 ID 的生成方式:
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" />
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<script type="text/javascript">
$(document).ready(function () {
$(function () {
$("#txtage").datepicker();
});
});
</script>
<td><asp:TextBox ID="txtage" runat="server" ClientIdMode="static" /></td>
您还没有显示用于确保 jQuery 和 jQuery UI 已加载到页面上的代码。
只需添加 .ClientID
和文本框 ID,如下所示。
JS
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.0/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.11.0/jquery-ui.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$(function () {
$('#<%=txtage.ClientID%>').datepicker();
});
});
我刚刚检查了一下,这个效果很好:
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="testDatePicker.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" />
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<script src="script.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox runat="server" ID="myTextBox"></asp:TextBox>
</div>
</form>
</body>
</html>
script.js
$(function() {
$("#myTextBox").datepicker();
});
我可能会建议您删除 ReadOnly
属性,因为日期选择器会尝试修改文本框,但不太确定。
Asp.Net 需要一个 属性 即 TextMode
加载一个日历,它必须等于 "Date"
- TextMode="Date"
.
示例:
<asp:TextBox ID="txtage" TextMode="Date" runat="server" ReadOnly="true"></asp:TextBox>