|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;