使用javascript显示div onchange of dropdownlist值

时间:2009-11-17 12:17:27

标签: javascript drop-down-menu

我有一个下拉列表,我想使用javascript显示 div onchange事件,但它说需要对象

aspx代码

<asp:DropDownList runat="server" ID="lstFilePrefix1" onchange="showTR();" >
    <asp:ListItem Text="Prefix1" Value="Prefix1" />
    <asp:ListItem Text="Prefix2" Value="Prefix2" />
    <asp:ListItem Text="Prefix3" Value="Prefix3" />
    <asp:ListItem Text="Prefix1 and Prefix2" Value="Prefix1 and Prefix2" />
    <asp:ListItem Text="Prefix2 and Prefix3" Value="Prefix2 and Prefix3" />
</asp:DropDownList>

和.js文件中的javascript代码

function showTR() {
    var dropdown = document.getElementById( "<%=lstFilePrefix1.ClientID%>" ); // Get a reference to the dropdown (select) element
    var selectedItemValue = dropdown.options[ dropdown.selectedIndex ].value; // use the dropdown reference to get the selected item's value
    var div2 = document.getElementById( "data" ); // Get a reference to div2

    if( selectedItemValue == 'Prefix2' ) {
        div2.style.dispaly= "block";// If the selectedItemValue is 'Action', show div2
    } else {
        div2.style.display = "none"; // Otherwise, hide div2
    }
}

2 个答案:

答案 0 :(得分:0)

好吧,我可能会弄错,但JavaScript不接受asp代码。当然,除非您使用正确的标头输出inside.js作为asp。你还有div2.style.dispaly

的版型
function showTR() {
    var dropdown = document.getElementById( "#anID" ); // Get a reference to the dropdown (select) element
    var selectedItemValue = dropdown.options[ dropdown.selectedIndex ].value; // use the dropdown reference to get the selected item's value
    var div2 = document.getElementById( "data" ); // Get a reference to div2

    if( selectedItemValue == 'Prefix2' ) {
        div2.style.display = "block";// If the selectedItemValue is 'Action', show div2
    } else {
        div2.style.display = "none"; // Otherwise, hide div2
    }
}

答案 1 :(得分:0)

使用

div2.style.visibility = "visible";

div2.style.visibility = "hidden";