我有3个文本框来检查输入的日期。我最初的代码是一个文本框。有没有办法将ID名称传递给onChange事件
<asp:TextBox ID="txtDate" runat="server" Width="110px" onChange="checkEnteredDate('txtDate')"></asp:TextBox>
function checkEnteredDate(var textBox = new String();) {
var inputDate = document.getElementById(textBox);
//if statement to check for valid date
var formatDate = new Date(inputDate.value);
if (formatDate > TodayDate) {
alert("You cannot select a date later than today.");
inputDate.value = TodayDate.format("MM/dd/yyyy");
}
}
答案 0 :(得分:2)
为了传递文本框的ID,您必须在Page_Load后面的代码中执行此操作:
txtDate.Attributes["onchange"] = String.Format("checkEnteredDate('{0}');",txtDate.ClientID);
答案 1 :(得分:2)
您可以将 this 作为onChange赋值的参数传递:
<asp:TextBox ID="txtDate" runat="server" Width="110px"
onChange="checkEnteredDate(this.id)"></asp:TextBox>
答案 2 :(得分:0)
我实际上已经明白了。我做了以下事情:
...onChange="checkEnteredDate(this)"...
function checkEnteredDate(inputDate) {
//if statement to check for valid date
var formatDate = new Date(inputDate.value);
if (formatDate > TodayDate) {
alert("You cannot select a date later than today.");
inputDate.value = TodayDate.format("MM/dd/yyyy");
}
}
getElement让我搞砸了。感谢您的建议,他们为我带来了正确的方向。