繁体   English   中英

调用javascript函数时出现“对象预期”错误

[英]“Object expected” error while calling javascript function

我想根据下拉列表中选择的值更改文本框的可见性。

我创建了这样的函数:

function ShowGiftCardSource() {
        var ddlGiftCardSource = document.getElementById('<%=ddlGiftCardSource.ClientID%>');
        var txtGiftCardSource = document.getElementById('<%=txtGiftCardSource.ClientID%>');

        if (ddlGiftCardSource.value == "Other") {
            txtGiftCardSource.style.visibility = "visible";
            txtGiftCardSource.focus();
        }
    }

在CS页面中:

ddlGiftCardSource.Attributes.Add("onChange", "OnSelectedIndexChanged();"); 

在控制中:

<asp:DropDownList ID="ddlGiftCardSource" runat="server" Width="151px" onChange="ShowGiftCardSource();">

但是我收到以下错误:

Microsoft JScript runtime error: Object expected

有人可以帮我解决一下吗?

也许那是因为你在onChange处理程序中使用ShowGiftCardOccasion()方法,但你的方法名是ShowGiftCardSource()? 然后javascript只是找不到正确名称的方法。

将代码更改为:

ddlGiftCardSource.Attributes.Add("onChange", "ShowGiftCardSource();");

并从标记中删除onchange

<asp:DropDownList ID="ddlGiftCardOccasion" runat="server" Width="151px">

标签中的onchange是要调用的服务器端方法。

编辑:如果你已经有服务器端方法,你必须首先添加AutoPostBack到下拉菜单然后在服务器端onchange事件显示文本框:

<asp:DropDownList ID="ddlGiftCardOccasion" runat="server" Width="151px" OnChange="ShowGiftCardSource" AutoPostBack="True">

在您的C#代码背后:

void ShowGiftCardSource(object sender, EventArgs e) {
  //code.....
  txtGiftCardSource.Visible = true;
}

当然,摆脱ddlGiftCardSource.Attributes.Add行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM