onTextChange事件没有触发我通过Java脚本进行的文本更改 [英] onTextChange event not firing against the the text change I made through Java script

查看:89
本文介绍了onTextChange事件没有触发我通过Java脚本进行的文本更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JavaScript

< script type =text / javascript>

document.getElementById( TextBox1)。innerText ='嗨';

< / script>



HTML

JavaScript
<script type="text/javascript">
document.getElementById("TextBox1").innerText= 'Hi";
</script>

HTML

<form id="form1" runat="server">
  <div>
    <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
  </div>
</form>





< b> 代码背后



Code Behind

protected void TextBox1_TextChanged(object sender, EventArgs e)
{
    string Query = string.Format("INSERT INTO GMaps(latLng) VALUES('{0}')", TextBox1.Text);
    SqlConnection cn = new SqlConnection("ConnectionString");
    SqlCommand cmd = new SqlCommand(Query, cn);
    cn.Open();
    cmd.ExecuteNonQuery();
    cn.Close();
}

推荐答案

要调用javascript,你必须添加

onblur = your_javascript_function如果您不希望回发从中返回false。



To call javascript you have to add
onblur="your_javascript_function" and if you don't want the postback return false from it.

<asp:textbox id="TextBox1" runat="server" autopostback="True" ontextchanged="TextBox1_TextChanged" onblur="return your_javascript_function()" xmlns:asp="#unknown">
</asp:textbox>





此外,此行



Also, this line

document.getElementById("TextBox1").innerText= 'Hi";



不起作用,因为html中的T​​extBox1 id不是TextBox1,但是类似于Page_Ctl01_Ctl02_TextBox1



ASP.NET确保所有控件ID都是唯一的nd服务器破坏你写的ID,所以它们是独一无二的,它们与父母一起预先添加



你可以访问这样的元素:


would not work due TextBox1 id in the html is NOT TextBox1, but something like Page_Ctl01_Ctl02_TextBox1

ASP.NET ensures that all control ids are unique and server mangles the ids you wrote so they are unique by prepending them with parents

You can access the element like this:

document.getElementById("<%=TextBox1.ClientID %>").innerText= 'Hi";





如果这有帮助,请花时间接受解决方案。谢谢。



If this helps please take time to accept the solution. Thank you.


这篇关于onTextChange事件没有触发我通过Java脚本进行的文本更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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