How do I enable/disable options in Select Box using jquery

前端 未结 3 1336
北海茫月
北海茫月 2021-01-17 12:03

I have a select box which contains the options and optgroup that are generated dynamically using php.Now when I select \"ALL\" all the other optgroup

相关标签:
3条回答
  • 2021-01-17 12:35

    This is kind of a strange thing to be doing but here's code that meets your requirements.

    $('select').on('change', function() {
        if (this.value == '-1') {
            $('optgroup option').prop('disabled', true);
        } else {
            $('optgroup option').prop('disabled', false);
        }
    });
    

    Live Example - http://jsfiddle.net/NpNFh/

    0 讨论(0)
  • 2021-01-17 12:48

    You can use two variations on the syntax for the disabled attribute, depending on the version of HTML you are targeting:

    HTML4: <option value="spider" disabled>Spider</option>
    XHTML: <option value="spider" disabled="disabled">Spider</option>
    
    0 讨论(0)
  • 2021-01-17 12:51

    You can use the following code.Let us consider you have three select boxes as follows

    <select class="sel_box" id="sel_1" onchange="disable_sel(this.value);" ></select>
    <select class="sel_box" id="sel_2" onchange="disable_sel(this.value);" ></select>
    <select class="sel_box" id="sel_3" onchange="disable_sel(this.value);" ></select>
    

    and in function let 'opt' is argument now use following

    $('.sel_box option[value="'+opt+'"]').attr("disabled", true);
    
    0 讨论(0)
提交回复
热议问题