Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javascript显示dropdownlist值的div onchange_Javascript_Drop Down Menu - Fatal编程技术网

使用javascript显示dropdownlist值的div onchange

使用javascript显示dropdownlist值的div onchange,javascript,drop-down-menu,Javascript,Drop Down Menu,我有一个dropdownlist,我想用javascript显示一个divonchange事件,但它说需要对象 aspx代码 <asp:DropDownList runat="server" ID="lstFilePrefix1" onchange="showTR();" > <asp:ListItem Text="Prefix1" Value="Prefix1" /> <asp:ListItem Text="Prefix2" Value="Pref

我有一个dropdownlist,我想用javascript显示一个divonchange事件,但它说需要对象

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

我可能弄错了,但是JavaScript不接受asp代码。当然,除非您将inside.js输出为具有正确标题的asp。在div2.style.dispaly上还有一个typeo

使用


如果代码被剪切并粘贴,则会出现键入错误:div2.style.dispaly应为div2.style.display。。。他的回答太短,不符合质量标准。请改进它,添加一些解释,以便它对社区有用,而不仅仅是对OP有用。
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
    }
}
div2.style.visibility = "visible";
div2.style.visibility = "hidden";