简体   繁体   English

如何根据第三个下拉列表项的选择切换不同的标签(下拉列表和列表)?

[英]how to switch with different tags ( dropdown and list ) depending on the selection of third dropdown's item?

I have 2 dropdownlist(lets say A and B) and 1 listItem (lets say C).我有 2 个下拉列表(比如说 A 和 B)和 1 个列表项(比如说 C)。 I need to switch between dropdown (B) and listitem (C) depending on option selected on first dropdown (A) which has only two options.我需要根据在只有两个选项的第一个下拉列表 (A) 上选择的选项在下拉列表 (B) 和列表项 (C) 之间切换。 How do you switch like this using javascript?你如何使用javascript像这样切换?

  <tr id="trInclude" runat="server">
        <td class="label" valign="top">Include : </td>
        <td valign="top">
            <asp:DropDownList ID="ddlExpiring" runat="server" Visible="true"  onchange="reloadDisplayBfys(this.options[this.selectedIndex].value);">
                <asp:ListItem Value="1" Text="Only Expiring"></asp:ListItem>
                <asp:ListItem Value="0" Text="ALL"></asp:ListItem>
            </asp:DropDownList>

            <asp:Label ID="lblFunds" runat="server" Text="Funds" CssClass="label"></asp:Label>
        </td>
    </tr>
    <tr>
        <td class="label" valign="top" width="300">
            <asp:Label ID="lblBFY" runat="server" Text="Expiring budget fiscal year:"></asp:Label></td>
        <td valign="top">
            <asp:ListBox ID="lstBudgetFiscalYear" runat="server" SelectionMode="Multiple" Rows="5" Visible="false">
                <asp:ListItem Text="All" Value="" class="all" />
            </asp:ListBox>
            <asp:DropDownList ID="ddlBudgetFiscalYear" runat="server" Visible="false" onchange="reloadFY(this.options[this.selectedIndex].value);">
                <asp:ListItem Text="All" Value="" class="all"></asp:ListItem>
            </asp:DropDownList>
        </td>
    </tr>


 function reloadDisplayBfys(index) {
        var bel = document.getElementById("<%= ddlExpiring.ClientID%>");
        var bfy;
        if (bel.selectedIndex) {
            bfy = bel.options[bel.selectedIndex].value;
        }
        if (bfy == 0) {
            document.getElementById("<%= lstBudgetFiscalYear.ClientID%>").style.display = "block";
            document.getElementById("<%= ddlBudgetFiscalYear.ClientID%>").style.display = "none"; 
        }
        else {
            document.getElementById("<%= lstBudgetFiscalYear.ClientID%>").style.display = "none";
            document.getElementById("<%= ddlBudgetFiscalYear.ClientID%>").style.display = "block";

        }
    }

If asp:DropDownList works just like an html select element, then your selector for the value looks right.如果 asp:DropDownList 就像一个 html select元素一样工作,那么您的值选择器看起来是正确的。 One way to use it would be to skip the "onchange" attribute and use a regular event listener like in the code below.使用它的一种方法是跳过“onchange”属性并使用常规事件侦听器,如下面的代码所示。


(Edited to meet the specifications in your response) (编辑以满足您的回复中的规格)
This solution uses a template element to store your lstFiscalYear option, which is conditionally inserted/removed: 此解决方案使用模板元素来存储您的lstFiscalYear选项,该选项是有条件地插入/删除的:

 const firstDropdown = document.getElementById("firstDropdown"), secondDropdown = document.getElementById("secondDropdown"); firstDropdown.addEventListener("change", updateDisplay); function updateDisplay(event){ const isInitialState = secondDropdown.options[0].value == "default", useInitialState = parseInt(firstDropdown.value) == 0; if(isInitialState && !useInitialState){ alternativeOption = document.getElementById("alternativeOptionTemplate").content.cloneNode(true); secondDropdown.insertBefore(alternativeOption, secondDropdown.options[0]); secondDropdown.selectedIndex = 0; } else if(!isInitialState && useInitialState){ secondDropdown.removeChild(secondDropdown.options[0]); secondDropdown.selectedIndex = 0; } else{ /* User selected neither 0 nor 1 */ } }
 <select id="firstDropdown"> <option value="0">Zero</option> <option value="1">One</option> </select> <template id="alternativeOptionTemplate"> <option value="lastFiscalYear">Info about last fiscal year</option> </template> <select id="secondDropdown"> <option value="default">Default</option> </select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM