Jquery adding and removing items from listbox

后端 未结 9 1022
粉色の甜心
粉色の甜心 2021-02-03 13:13

I\'ve created this fiddle, it allows the user to click on either art or video, dynamically populating the the second listbox with the list associated with those selections. Ther

9条回答
  •  既然无缘
    2021-02-03 13:22

    I think you would want to do something like this: Check if value is in select list with JQuery.

    Modifying your code to something like this should work:

    $("#SelectBox2 option:selected").each(function () {
        var optionVal = $(this).val();
        var exists = false;
        $('#SelectedItems option').each(function(){
            if (this.value == optionVal) {
                exists = true;
            }
        });
    
        if(!exists) {
            inHTML += '';
        }
    });
    

    Removing selected items would look like this:

    $('#remove').click(function () {
        $("#SelectedItems option:selected").remove();
    });
    

提交回复
热议问题