日期:2014-05-18  浏览次数:20415 次

怎样给DropDownList添加js的onchange事件
我添加了后说不是有效属性

------解决方案--------------------
不要用 <asp:DropDownList> </asp:DropDownList> 这个,你用 <select> </select> 这个,
那个好像有过滤的,你给他加客户端的属性会被过滤掉的。如果 <select> </select> 这个要在服务器端用加个 runat= "server " 属性就行。其他用法基本一样的。
------解决方案--------------------
ddlUserImg.Attributes.Add( "onchange ", "getImg() ");
我写的个例子
你参考一下!
------解决方案--------------------
<script language= "javascript " type= "text/javascript ">

function getImg()
{
var obj = document.getElementById( "ddlUserImg ");
var img = document.getElementById( "img ");
img.src = obj.value;
}

</script>


<body onload= "getImg() ">


这是前台的一段相关代码
------解决方案--------------------
不要用 <asp:DropDownList> </asp:DropDownList> 这个,你用 <select> </select> 这个,
那个好像有过滤的,你给他加客户端的属性会被过滤掉的。如果 <select> </select> 这个要在服务器端用加个 runat= "server " 属性就行。其他用法基本一样的。
-----------------
说明的是通常加入是在后台利用attributes.add加入的,而不是在前台设计的html里面写上去的
------解决方案--------------------
我添加了后说不是有效属性
=====
不管它,这是 IDE 的对文档的验证,asp。net 会正确处理,运行时照样使用
当然,添加的属性(Attribute)是要受原声 html 控件支持才行

<asp:dropdownlist id=MyDropDownList onchange= "foo() " ....

等同

MyDropDownList.Attributes[ "onchange "] = "foo() ";