Javascript 如何使用asp.net从文本框调用函数?
我一直在尝试调用函数Javascript 如何使用asp.net从文本框调用函数?,javascript,asp.net,Javascript,Asp.net,我一直在尝试调用函数formattate()。我试着把它放在text=”“/value=”“中,但它没有正确返回 我怎样才能解决这个问题 <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1 Runat="Server"> <script type="text/javascript"> function formatDate(date) {
formattate()
。我试着把它放在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>
这可能是您问题的解决方案。我还没有测试过,但我正在用同样的东西工作。我只是以另一种方式来做,使用带有jQuery和ASP服务器端代码的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?很有趣..让我试试。jQuery让它变得容易多了。如果你愿意的话,可以用纯JS来完成吗?我刚刚添加了另一段代码,只是为了说明它可以用纯JS的jQuery来完成。最好的情况是,我添加了另一个文本框用于这两种方法。我希望我能帮你解决你的问题;)
<asp:Textbox id="txtReport" ClientIDMode="Static" Runat="Server" Width="250px"></asp:Textbox>
<%@ 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>