Select2: Hide certain options dynamically

前端 未结 10 804
情深已故
情深已故 2020-12-03 06:56

Basically what I\'m looking for is the ability to hide options from the dropdown of select items. So, technically they would still be options, but you just wouldn\'t be able

10条回答
  •  余生分开走
    2020-12-03 07:03

    $('.selector').remove(); 
    

    And then append the option according to position it appear in select2 :

    To appear as first option then :

    $('.selector')
    .prepend('');
    

    To appear as last option then :

    $('.selector')
    .append('');
    

    To appear as after any of the option then :

    $('')
    .insertAfter($('.selector option:first-child'));
    

    Or

    $('')
    .insertAfter($('.selector > .option-class'));
    

    OR

    We can disable/enable the option using jquery, instead of hiding.

    $('.selector').prop('disabled', 'disabled');
    
    $('.selector')
    .prop('disabled', !$('.selector').prop('disabled'));
    

提交回复
热议问题