最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - KeyPress Event of TextBox in ASP.NET - Stack Overflow

programmeradmin1浏览0评论

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>

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>
Share Improve this question asked Feb 19, 2017 at 7:25 nikorionikorio 6914 gold badges17 silver badges28 bronze badges 3
  • Possible duplicate of How to make keypress event of asp textbox? – santosh singh Commented Feb 19, 2017 at 7:35
  • 1 please add aspx code too – Usman Commented Feb 19, 2017 at 7:38
  • 1 what error are you getting in console? – Ram Segev Commented Feb 19, 2017 at 7:38
Add a ment  | 

3 Answers 3

Reset to default 2

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./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.

发布评论

评论列表(0)

  1. 暂无评论