“预期目标”调用 javascript 函数时出错
我想根据下拉列表中选择的值更改文本框的可见性。
我创建了这样的函数:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将后面的代码更改为:
并从标签中删除
onchange
:标签中的
onchange
是要调用的服务器端方法。编辑:如果您已经有服务器端方法,则必须首先将 AutoPostBack 添加到下拉列表中,然后在服务器端 onchange 事件中显示文本框:
在后面的 C# 代码中:
当然,删除 ddlGiftCardSource。 Attributes.Add 行。
Change the code behind to:
And remove the
onchange
from the tag: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:
And in your C# code behind:
And of course, get rid of the
ddlGiftCardSource.Attributes.Add
line.也许那是因为您在 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.