简体   繁体   中英

Fetch filtered data from the drop down list and present in another drop down list

I have a dropdown list:

<asp:DropDownList ID="DropDownList1" runat="server" Width="222px">
    <Items>
        <asp:ListItem Text="Option 1" Value="1" />
        <asp:ListItem Text="Option 2" Value="2" />
        <asp:ListItem Text="Option 3" Value="3" />
        <asp:ListItem Text="Option 4" Value="4" />
        <asp:ListItem Text="Option 5" Value="5" />
        <asp:ListItem Text="Option 6" Value="6" />
        <asp:ListItem Text="Option 7" Value="7" />
    </Items>
</asp:DropDownList>

I want to create a dropdown list which takes all the items of the above mentioned dropdown items except for the one that is being selected(above). For ex: I have option 4 selected in the above drop down list and i want to present options from 1 to 7(except option 4) as the list of items for the second dropdown.

Can someone tell me how to achieve it?

I have used the below code to clone the dropdown list values to other dropdown list values.

<script type="text/javascript">
$(function() {
$("#btnclone").click(function() {
$('#DropDownList1').clone().attr('id', 'choices_' + $(this).index()).insertAfter("#DropDownList1");
});
});
</script>

But can anybody suggest how to show all the values except the selected value from the previous dropdown list?

This one worked for me:

 <script type="text/javascript">
    $('#btnclone').click(function () {
        var original = $('select.selService:eq(0)');
        var allSelects = $('select.selService');
        var clone = original.clone();

        $('option', clone).filter(function (i) {
            return allSelects.find('option:selected[value="' + $(this).val() + '"]').length;
        }).remove();

        $('#target').append(clone).append('<br />');
    });
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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