读书人

|M| 在网上考下来的一个操作ListBox的

发布时间: 2012-01-23 21:57:28 作者: rapoo

|M| 在网上考下来的一个操作ListBox的JS代码 无法实现options(index).value出错。要怎么改 谢谢
前台 <asp:ListBox ID= "list_Type " runat= "server " Height= "220px " Width= "174px ">
后台list_Type.Attributes[ "ondblclick "] = "SelectType(); ";

JS:
<script type= "text/javascript ">
function SelectType()
{
var listbox_Type = document.createElement( ' <%=list_Type.ClientID %> ');
var index = document.getElementById( ' <%=list_Type.ClientID %> ').selectedIndex;
alert(index);
if(listbox_Type.selectedIndex != -1)
{
alert(listbox_Type.options(index).value); 这里出错
alert(listbox_Type.options(index).text); 这里也出错

document.getElementById( " <%=txt_TypeName.ClientID %> ").text=listbox_Type.options(index).text;
document.getElementById( " <%=txt_TypeID.ClientID %> ").value=listbox_Type.options(index).value;
}
}
</script>

[解决办法]
改成这样:
alert(listbox_Type.options[index].value);
alert(listbox_Type.options[index].text);
[解决办法]
改成这样,搞定:
<script type= "text/javascript ">
function SelectType()
{
var listbox_Type = document.createElement( ' <%=list_Type.ClientID %> ');
var index = document.getElementById( ' <%=list_Type.ClientID %> ').selectedIndex;
if(index != -1)
{
document.getElementById( " <%=txt_TypeName.ClientID %> ").value=document.getElementById( ' <%=list_Type.ClientID %> ').options[index].text;
document.getElementById( " <%=txt_TypeID.ClientID %> ").value=document.getElementById( ' <%=list_Type.ClientID %> ').options[index].value;


}
}
</script>
[解决办法]
var listbox_Type = document.createElement( ' <%=list_Type.ClientID %> ');
-----------------------------
这句的问题,用getelementbyid就可以的
[解决办法]
var listbox_Type = document.all. <%=list_Type.ClientID %> 或 var listbox_Type = document.getElementById( ' <%=list_Type.ClientID %> ');

读书人网 >asp.net

热点推荐