我有2个文本框,我试图捕获onchange事件。这些文本框位于asp.net更新面板中的用户控件内。每次更改文本框文本时,都会收到“Microsoft JScript运行时错误:预期的对象”错误,但没有提供任何其他有用的信息。为什么textbox onchange事件在asp.net用户控件内不能正确触发?
下面是代码:
<script type="text/javascript">
function ResetOrientationImage() {
var width = (+document.getElementById('<%= txtWidth.ClientID %>').value);
var height = (+document.getElementById('<%= txtHeight.ClientID %>').value);
if (width > height) {
document.getElementById('<%= imgOrientation.ClientID %>').src = "images/buttons/Landscape.png";
} else {
document.getElementById('<%= imgOrientation.ClientID %>').src = "images/buttons/Portrait.png";
}
}
</script>
<asp:TextBox ID="txtWidth" runat="server" Width="50px" onchange="ResetOrientationImage()" Text="0"></asp:TextBox>
<asp:TextBox ID="txtHeight" runat="server" Width="50px" onchange="ResetOrientationImage()" Text="0"></asp:TextBox>
<asp:Image ID="imgOrientation" ImageUrl="~/images/buttons/Portrait.png" runat="server" />
我想这一定是使用JavaScript的一个问题,但我把这个确切的代码到一个独立的.aspx页面中并没有错误正确执行。
编辑 查看Firebug时,真正的错误是在页面执行时找不到ResetOrientationImage函数。如果我将用户控件中的JavaScript移动到页面上,它就会像我所期望的那样工作。为什么JavaScript不能在用户控件中生存?
什么东西搞乱了你在其他地方的JavaScript,在页面中寻找一个错误,你正在添加用户控件 – GregM 2012-02-15 20:32:12
你看过它与IE8 +的JavaScript控制台指向错误?你调试了什么'document.getElementById('<%= txtWidth.ClientID%>').value'和另一个返回? – epascarello 2012-02-15 20:34:22