admin管理员组文章数量:1428600
I have 3 textboxes to do a check on the entered date. The code I originally had was for one textbox. Is there a way to pass an ID name to the onChange event
<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");
}
}
I have 3 textboxes to do a check on the entered date. The code I originally had was for one textbox. Is there a way to pass an ID name to the onChange event
<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");
}
}
Share
Improve this question
edited Jul 24, 2009 at 18:57
Chetan S
23.8k2 gold badges66 silver badges78 bronze badges
asked Jul 24, 2009 at 18:47
MrMMrM
22.1k31 gold badges116 silver badges142 bronze badges
3 Answers
Reset to default 2In order to pass the Id of the textbox you'll have to do this in your code behind's Page_Load:
txtDate.Attributes["onchange"] = String.Format("checkEnteredDate('{0}');",txtDate.ClientID);
You can pass this as the parameter on the onChange assignment:
<asp:TextBox ID="txtDate" runat="server" Width="110px"
onChange="checkEnteredDate(this.id)"></asp:TextBox>
I actually figured it out. I did the following:
...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 was messing me up. Thanks for the suggestions, they brought me in the right direction.
本文标签: aspnetCan i pass the ID name in an onChange event for a textboxStack Overflow
版权声明:本文标题:asp.net - Can i pass the ID name in an onChange event for a textbox? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745531929a2662100.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论