我一直试图调用函数formatDate()
。我尝试将其text = ""
/ value = ""
,但它没有正确返回。
我该如何解决这个问题?
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1 Runat="Server">
<script type="text/javascript">
function formatDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
year = year.toString().substr(2, 2);
dateRecord = [year, month].join('/');
return dateRecord
}
</script>
<h2><asp:Label ID="lblPageName" Text="Project Seq Code" runat="server" /></h2>
<table width="625" cellpadding="0" cellspacing="1" style="margin-left:180px;">
<tr>
<td> Report </td>
<td><asp:Textbox id="txtReport" text="return formateDate(date)" Runat="Server" Width="250px" />
</td>
</tr>
</table>
</asp:Content>
答案 0 :(得分:1)
这可能是您问题的解决方案。我没有测试过,但我正在做同样的事情。我只是这样做,使用带有jQuery和ASP Server端代码的HTML输入来访问这些控件。
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1 Runat="Server">
<script type="text/javascript">
function formatDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
year = year.toString().substr(2, 2);
dateRecord = [year, month].join('/');
$("#txtReport").val(dateRecord);
}
</script>
<h2><asp:Label ID="lblPageName" Text="Project Seq Code" runat="server" /></h2>
<table width="625" cellpadding="0" cellspacing="1" style="margin-left:180px;">
<tr>
<td> Report </td>
<td><asp:Textbox id="txtReport" ClientIDMode="Static" Runat="Server" Width="250px" />
</td>
</tr>
</table>
</asp:Content>
尝试使用jQuery放置文本,而不是调用函数来放置文本(我甚至不知道它是否可能)。
您可能需要将ASP控件更改为:
<asp:Textbox id="txtReport" ClientIDMode="Static" Runat="Server" Width="250px"></asp:Textbox>
ClientIDMode将确保使用分配给您的元素的ID,而不是ASP中生成的ID。
修改强> 我刚测试了这个,这是我的代码。它没有问题,唯一的区别是我的“日期”变量是硬编码的,因为我需要一个例子。 我还包含了jQuery的链接,并将脚本代码放在标题中。 这是你想要的吗?
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="http://code.jquery.com/jquery-2.1.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
var date = "02/03/1994";
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
year = year.toString().substr(2, 2);
var dateRecord = [year, month].join('/');
//Placing your date with jQuery
$(document).ready(function () {
$("#txtReport").val(dateRecord + " - this was done with jQuery");
});
//Placing your date with pure Javascript
window.addEventListener("load", function() {
document.getElementById("txtReportJS").value = dateRecord + " - this was done with pure JS";
}, false);
</script>
</head>
<body>
<form id="form1" runat="server">
<h2>
<asp:Label ID="lblPageName" Text="Project Seq Code" runat="server" /></h2>
<table width="625" cellpadding="0" cellspacing="1" style="margin-left: 180px;">
<tr>
<td> Report </td>
<td>
<asp:TextBox ID="txtReport" ClientIDMode="Static" runat="Server" Width="250px" />
<asp:TextBox ID="txtReportJS" ClientIDMode="Static" runat="Server" Width="250px" />
</td>
</tr>
</table>
</form>
</body>
</html>
<小时/> 我使用jQuery和纯Javascript方法编辑我的示例以将文本设置为TextBox;)我希望这是您需要的,如果是,请标记为答案:)