bootstrap multiselect get selected values

前端 未结 7 1520
难免孤独
难免孤独 2021-01-31 16:39

How do I get back on onChange all selected values in my multiselect dropdown. Used plugin here. Trying to use the following but I think I\'m on the wrong track

$         


        
7条回答
  •  一向
    一向 (楼主)
    2021-01-31 17:15

    $('#multiselect1').on('change', function(){
        var selected = $(this).find("option:selected");    
        var arrSelected = [];
    
        // selected.each(function(){
        //   arrSelected.push($(this).val());
        // });
    
        // The problem with the above selected.each statement is that  
        // there is no iteration value.
        // $(this).val() is all selected items, not an iterative item value.
        // With each iteration the selected items will be appended to 
        // arrSelected like so    
        //
        // arrSelected [0]['item0','item1','item2']
        // arrSelected [1]['item0','item1','item2'] 
    
        // You need to get the iteration value. 
        //
        selected.each((idx, val) => {
            arrSelected.push(val.value);
        });
        // arrSelected [0]['item0'] 
        // arrSelected [1]['item1']
        // arrSelected [2]['item2']
    });
    

提交回复
热议问题