jQuery prevent change for select

前端 未结 13 1388
無奈伤痛
無奈伤痛 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:32

    This was the ONLY thing that worked for me (on Chrome Version 54.0.2840.27):

    $('select').each(function() {
      $(this).data('lastSelectedIndex', this.selectedIndex);
    });
    $('select').click(function() {
      $(this).data('lastSelectedIndex', this.selectedIndex);
    });
    
    $('select[class*="select-with-confirm"]').change(function() {  
      if (!confirm("Do you really want to change?")) {
        this.selectedIndex = $(this).data('lastSelectedIndex');
      }
    });
    
    
    
    
    

提交回复
热议问题