How to disable multiple select options

前端 未结 2 781
时光取名叫无心
时光取名叫无心 2021-01-24 18:56

How to disable multiple options in one select. For example i have 2 selects with 5 and 3 options. So if i select 3rd option in second select, i want to disable 3rd,4th and 5th o

2条回答
  •  悲&欢浪女
    2021-01-24 19:13

    You can just hide them :

    var ary = [3, 4, 5];
    $('[name=smjer] option').filter(function () {
        return ($.inArray(parseInt(this.value), ary) > -1);
    }).hide();
    

    and when again required show it:

    var ary = [3, 4, 5];
    $('[name=smjer] option').filter(function () {
        return ($.inArray(parseInt(this.value), ary) > -1);
    }).show();
    

    To make it even better, you can do this:

    var ary = [3, 4, 5];
    
    // Get the options to be hidden/shown
    var $options = $('[name=smjer] option').filter(function () {
        return ($.inArray(parseInt(this.value), ary) > -1);
    });
    
    // Hide the options
    $options.hide();
    
    // Show the options
    $options.show();
    

提交回复
热议问题