如何在 asp:TextBox 的 keyup 事件上调用 javascript 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5607313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 18:01:45  来源:igfitidea点击:

How to call javascript function on keyup event for asp:TextBox

javascriptasp.neteventsclient-side

提问by jams

How to call javascript function on keyup event for asp.net's TextBox control? I am trying something like this, but it is not working.

如何在 asp.net 的 TextBox 控件的 keyup 事件上调用 javascript 函数?我正在尝试这样的事情,但它不起作用。

<asp:TextBox ID="txt_userid" runat="server"  onkeyup="GetRes();"></asp:TextBox>


UPDATE
there is a update alert is working but breakpoints in java function is not working.


UPDATE
有更新警报正在运行,但 java 函数中的断点不起作用。

回答by Afshin Gh

Test this solution:

测试此解决方案:

Add this code to your page load:

将此代码添加到您的页面加载中

txt_userid.Attributes.Add("onkeyup", "GetRes();");

I don't say this is best way possible, but it works.

我不是说这是最好的方法,但它有效。

UPDATE:

更新:

Complete sample:

完整样本:

ASPX:

ASPX:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:TextBox ID="txt_userid" runat="server"></asp:TextBox>
    </div>
    </form>
</body>
</html>

Code behind:

后面的代码:

protected void Page_Load(object sender, EventArgs e)
        {
            txt_userid.Attributes.Add("onkeyup", "alert('hi');");
        }

Works perfectly. Tested it in IE, Chrome, FireFox.

完美运行。在 IE、Chrome、FireFox 中对其进行了测试。

回答by mohit

it is very simple:

这很简单:

TextBox1.Attributes.Add("onclick", "hello();");

in asp.cs code file.

在 asp.cs 代码文件中。

then in aspx source file use javascript function:

然后在 aspx 源文件中使用 javascript 函数:

<script type="text/javascript">
    function hello() {

    alert("hi")

    }

</script>

回答by suraj mahajan

$(document).ready(function () {
  $('#<%=TextBox1%>').keyup(function () {
    updateDateKey($(this).val());
  });
});