“预期目标”调用 javascript 函数时出错

发布于 2024-10-19 00:16:27 字数 851 浏览 5 评论 0原文

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

我创建了这样的函数:

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

有人可以帮我解决它吗?

I want to change the visibility of a textbox, according to the value selected in a dropdownlist.

I have created the function like this:

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

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

In the CS Page:

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

and in the control:

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

But I'm getting the following error:

Microsoft JScript runtime error: Object expected

Could some one please help me to resolve it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

舞袖。长 2024-10-26 00:16:27

将后面的代码更改为:

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 行。

Change the code behind to:

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

And remove the onchange from the tag:

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

The onchange in the tag is the server side method to call.

Edit: in case you already have server side method you must first add AutoPostBack to the drop down then in the server side onchange event show the textbox:

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

And in your C# code behind:

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

And of course, get rid of the ddlGiftCardSource.Attributes.Add line.

梦太阳 2024-10-26 00:16:27

也许那是因为您在 onChange 处理程序中使用 ShowGiftCardOccasion() 方法,但您的方法名称是 ShowGiftCardSource() ?那么 javascript 就无法找到具有正确名称的方法。

Maybe thats because you are using ShowGiftCardOccasion() method in onChange handler, but you method name is ShowGiftCardSource() ? Then javascript just cannot find method with correct name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文