第一个TEXTBOX是让用户输入时间的小时,限制只能输入数字00-23,第二个TEXTBOX是让用户输入时间的分钟,限制只能输入数字00-59,可是运行时却报错误:
CS0103: The name 'txtHour1' does not exist in the current context
CS0103: The name 'txtMin1' does not exist in the current context
前端代码:
<asp:TextBox ID="txtHour1" runat="server" Width="20px" MaxLength="2" TextMode="SingleLine" rows = "1" visible="false" style="font-size:13pt;font-family: MingLiU_HKSCS,MingLiU,Vernada;ime-mode:disabled;" onkeydown="if(event.keyCode==13)event.keyCode=9" onKeyPress="if ((event.keyCode<48 || event.keyCode>57)) event.returnValue=false" onchange="validateHH();" Text="<%# Bind('AssignmentDateHH') %>" ></asp:TextBox> <asp:TextBox ID="txtMin1" class="timeTextBox" runat="server" Width="22px" MaxLength="2" TextMode="SingleLine" rows = "1" visible="false" style="font-size:13pt;font-family: MingLiU_HKSCS,MingLiU,Vernada;ime-mode:disabled;" onkeydown="if(event.keyCode==13)event.keyCode=9" onKeyPress="if ((event.keyCode<48 || event.keyCode>57)) event.returnValue=false" onchange="validateMM();" Text="<%# Bind('AssignmentDateMM') %>"></asp:TextBox>
JS
function validateHH() { var regHH = new RegExp("^[0-23]*$"); var objHH = document.getElementById("<%=txtHour1.ClientID %>"); if (!regHH.test(objHH.value)) { alert("请输入00-23"); } } function validateMM() { var regMM = new RegExp("^[0-59]*$"); var objMM = document.getElementById("<%=txtMin1.ClientID %>"); if (!regMM.test(objMM.Value)) { alert("请输入00-59"); } }
花两个小时看下jquery。简单的很
你能看看我的js写的哪儿不对吗?总是错误提示:CS0103: The name 'textHour1' does not exist in the current context
function fx() { var o = document.getElementById('<%=textHour1.ClientID %>').value; if(parseInt(o)>23|| parseInt(o) <0)){ alert("please input 00-23"); document.getElementById('<%=textHour1.ClientID %>').focus(); } }
对了,我的这个控件是在gridview里面,gridview里面的控件怎么拿到id啊?
@rivahuang: 你js放到页面底部
<asp:GridView ID="gvContentAssign1" runat="server" AutoGenerateColumns="false" AllowPaging="false" EmptyDataText="null" Width="100%" OnRowDataBound="gvRowDatabound" OnRowEditing="gvRowEditing"> <Columns> <asp:TemplateField HeaderText="<%$ resources:AssignmentDate %>" > <ItemTemplate > <asp:TextBox ID="txtHour1" ClientIDMode="Static" runat="server" Width="20px" MaxLength="2" TextMode="SingleLine" rows = "1" visible="false" style="font-size:13pt;font-family: MingLiU_HKSCS,MingLiU,Vernada;" ime-mode="disabled" onkeydown="if(event.keyCode==13)event.keyCode=9" onKeyPress="if ((event.keyCode<48 || event.keyCode>57)) event.returnValue=false" Text="<%# Bind('AssignmentDateHH') %>" ></asp:TextBox> <asp:TextBox ID="txtMin1" ClientIDMode="Static" runat="server" Width="22px" MaxLength="2" TextMode="SingleLine" rows = "1" visible="false" style="font-size:13pt;font-family: MingLiU_HKSCS,MingLiU,Vernada;" ime-mode="disabled" onkeydown="if(event.keyCode==13)event.keyCode=9" onKeyPress="if ((event.keyCode<48 || event.keyCode>57)) event.returnValue=false" Text="<%# Bind('AssignmentDateMM') %>"></asp:TextBox> </ItemTemplate> <ItemStyle Width="4%" /> <HeaderStyle Width="4%" /> </asp:TemplateField> </Column> </GridView>
因为控件是在gridview里面,所以<%=btn.ClientID%>就失效了。所以给控件增加ClientIDMode="Static"属性,然后就可以获取gridview里面的控件的id了。再增加onkeydown="if(event.keyCode==13)event.keyCode=9" onKeyPress="if ((event.keyCode<48 || event.keyCode>57)) event.returnValue=false"可以限制用户只能输入数字。
jquery
$(document).ready(function () { $("#txtHour1").live('focusout', function () { if (!/^\d+$/.test($("#txtHour1").val()) || $("#txtHour1").val() > 23 || $("#txtHour1").val() < 0) { alert("請輸入兩位數字正確時間格式00-23"); }; }); });