ASP.Net - TextBox_TextChanged不起作用 [英] ASP.Net - TextBox_TextChanged not working

查看:81
本文介绍了ASP.Net - TextBox_TextChanged不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Asp.Net项目中,我尝试使用JavaScript来捕获文本更改。相关代码如下:



In my Asp.Net project, I tried to use JavaScript to catch the text change. the related code are below:

<script type="text/javascript">
     function txtMsg_TextChanged(txt) {
         //alert("Hi!");
         var s = txtMessage.toString();
     }
</script>




<asp:TextBox ID="txtMessage"

                runat="server"

                Columns="40"

                Rows="6" AutoPostBack="True"

                TextMode="MultiLine" ontextchanged="txtMessage_TextChanged"

                onchange="txtMsg_TextChanged(this)" >
</asp:TextBox>



在Code背后,Page_Loa d()


In Code behind, Page_Load()

if (!Page.IsPostBack) {
    txtMessage.Attributes.Add("onchange", "txtMsg_TextChanged();
}"



当我在函数txtMsg_TextChanged(txt)上调试代码时,我收到了警告。然后我检查了两个变量:txt和txtMessage - 它们都没有值。如何获取我在文本框中键入的txtMessage的值?谢谢。


When I debugged the code on the function txtMsg_TextChanged(txt), I got a warning. Then I checked both variables: txt, and txtMessage - both of them have no value. How can get the value of txtMessage that I typed in the textbox? Thanks.

推荐答案

在头部添加此脚本部分:

Add this script in the head section:
<script type="text/javascript">
function ChangeText(obj) {
    alert(obj.value);
}
</script>



这是文本框的标记:


This is the markup for the textbox:

<asp:textbox id="myTextBox" runat="server" autopostback="true" ontextchanged="myTextBox_TextChanged"></asp:textbox>



这个是后面的代码:


And this is the code behind:

if (!IsPostBack)
{
    myTextBox.Attributes.Add("onchange", "ChangeText(this);");
}
protected void myTextBox_TextChanged(object sender, EventArgs e)
{
    Response.Write(myTextBox.Text);
}




如果你想在javascript端捕获文本更改事件,你有两个选择。

1.使用onchange事件。当你从文本框中松开焦点时,此事件将触发javascript函数。

2.使用keyup事件,它将在每个按键事件上触发javascript函数。



注意: - 由于您不想解雇服务器端事件,请不要在文本框中使用AutoPostBack =true。

Hi,
As you want to catch text change event on javascript side, you have two options.
1. Use onchange event. This event will fire javascript function as you loose focus from textbox.
2. Use keyup event, it will fire javascript function on each and every key up event.

Note:- As you don't want to fire server side event, do not use AutoPostBack="true" in textbox.
<!-- Your Markup-->
<asp:textbox id="txtMessage" runat="server" columns="40" rows="6" textmode="MultiLine" onchange="txtMsg_TextChanged(this)" ></asp:textbox>
<asp:textbox id="txtMessage1" runat="server" columns="40" rows="6" textmode="MultiLine" onkeyup="txtMsg_TextChanged1(this)" ></asp:textbox>

<script type="text/javascript">
     // this function is called as you loose focus from textbox "txtMessage"
     function txtMsg_TextChanged(a) {
         alert(a.value);
     }

     // this function is called on every keyup event of textbox "txtMessage1"
     function txtMsg_TextChanged1(a) {
         alert(a.value);
     }
</script>



尝试两种方法,根据需要使用。

希望它对你有所帮助。

谢谢。


Try both, use as per your need.
Hope it helps you.
Thanks.


使用Jquery按ID获取元素或者也可以使用裸javascript完成。



Use Jquery Get Element By ID or It can also be done with bare javascript.

function txtMsg_TextChanged(txt) {
        //alert("Hi!");
        var s =


这篇关于ASP.Net - TextBox_TextChanged不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆