jQuery prevent change for select

前端 未结 13 1286
無奈伤痛
無奈伤痛 2020-11-28 10:51

I want to prevent a select box from being changed if a certain condition applies. Doing this doesn\'t seem to work:

$(\'#my_select\').bind(\'change\', funct         


        
相关标签:
13条回答
  • 2020-11-28 11:44

    In the event someone needs a generic version of mattsven's answer (as I did), here it is:

    $('select').each(function() {
        $(this).data('lastSelected', $(this).find('option:selected'));
    });
    
    $('select').change(function() {
        if(my_condition) {
            $(this).data('lastSelected').attr('selected', true);
        }
    });
    
    $('select').click(function() {
        $(this).data('lastSelected', $(this).find('option:selected'));
    });
    
    0 讨论(0)
提交回复
热议问题