我想在asp文本框中设置值

时间:2012-08-09 05:12:27

标签: asp.net

我已经进行了很多搜索,但仍然没有得到答案,这是我的javascript。

function isNumeric(sender,keyCode) {
if (sender.value.length == 3 && res == true){
       console.log(sender.value.length);
            if(sender.value <1950 || sender.value > 2100){
          GY.style.visibility = "visible";
            document.getElementById('<%=GYValidatorLB.ClientID%>').value= "Please put Number in range A.D."
            }
       } 
    return res;
}

这是我的HTML代码

<td style="height: 26px"><asp:TextBox ID="GraduateYear_RQ"  onkeydown="return isNumeric(this,event.keyCode);" onkeyup="keyUP(event)" runat ="server" MaxLength="4" </asp:TextBox>
      <asp:Label ID="GYValidatorLB" text="โปรดใส่เฉพาะตัวเลข" runat="server" BackColor="White" BorderColor="White" ForeColor="Red" style="visibility:hidden"></asp:Label></td>

当用户键入AD范围之外的数字时,我想在GyvalidatorLB中设置值

提前感谢

1 个答案:

答案 0 :(得分:0)

由于GYValidatorLB位于<Table><TD>内,您必须使用innerHTML代替.value

你的功能应该是这样的:

function isNumeric(sender, keyCode) {
        var GY = document.getElementById('GYValidatorLB');
        if (sender.value.length >= 3) {
            console.log(sender.value.length);
            if (sender.value < 1950 || sender.value > 2100) {
                GY.style.visibility = "visible";
                GY.innerHTML = "Please put Number in range";
            }
            else {
                GY.style.visibility = "visible";
                GY.innerHTML = "";
            }
        }
        return res;
}

你的HTML:

<td style="height: 26px">
    <asp:TextBox ID="GraduateYear_RQ" onkeydown="return isNumeric(this,event.keyCode);" onkeyup="keyUP(event)" runat="server" MaxLength="4"> </asp:TextBox>
    <asp:Label ID="GYValidatorLB" Text="โปรดใส่เฉพาะตัวเลข" runat="server" BackColor="White" BorderColor="White" ForeColor="Red" Style="visibility: hidden"></asp:Label>
</td>