首页新闻找找看学习计划

JS不能获取到asp.net前端控件id,怎么限制textbox只能输入数字且范围为00-23?

0
悬赏园豆:10 [待解决问题]

第一个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");
        }
    }
rivahuang的主页 rivahuang | 初学一级 | 园豆:91
提问于:2015-11-27 17:33
< >
分享
所有回答(2)
0

花两个小时看下jquery。简单的很

茂茂 | 园豆:2860 (老鸟四级) | 2015-11-28 14:55

你能看看我的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();
        }
    }
支持(0) 反对(0) rivahuang | 园豆:91 (初学一级) | 2015-11-30 11:28

对了,我的这个控件是在gridview里面,gridview里面的控件怎么拿到id啊?

支持(0) 反对(0) rivahuang | 园豆:91 (初学一级) | 2015-11-30 11:53

@rivahuang: 你js放到页面底部

支持(0) 反对(0) 茂茂 | 园豆:2860 (老鸟四级) | 2015-11-30 17:30
0
<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");
            };
        });
    });
rivahuang | 园豆:91 (初学一级) | 2015-12-01 12:07
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册