简体   繁体   中英

KeyPress Event of TextBox in ASP.NET

I'm trying to get KeyPress Event for TextBox in ASP.NET, so I found this script, but it does not works for me

<title></title>

    <script src="jquery.js" type="text/javascript"></script>
    <script type="text/javascript"  lang="js">
        $(function () {
            $("#<%=TextBox1.ClientID %>").keypress(function () {
                alert("Wow; Its Work!.")
            });
        });
    </script> 

</head>

your script is working very well the problem might be in jquery file its not found in the given location you can change the src for check

<script src="jquery.js" type="text/javascript"></script>

To

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

try this

<asp:TextBox ID="TextBox1" runat="server" onkeypress="myFunction()"></asp:TextBox>

 <script language="javascript">
    function myFunction()
    {
        alert('Key Press')
    }
</script>

Like Mostafa wrote, it is important to insert the script at the END of the page. So that the page can register the controls first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM