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
Add a ment  | 

3 Answers 3

Reset to default 2

In 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