click event for option doesn't work in IE

前端 未结 7 2008
一整个雨季
一整个雨季 2020-12-16 21:11

I have a multiple select tag, and I need to write the function onclick of it\'s options, because I need to get the value of last clicked option, but when I wrote the followi

7条回答
  •  暖寄归人
    2020-12-16 22:06

    I liked @Reigel's (accepted) answer but needed to improve it for use in one of my projects. In the code below, I introduce a new function "findClickedOption" that accounts for the fact that maybe a user will re-click an already-selected option. This still doesn't account for CTRL-clicking multiple options but is good enough for me.

        var multiselect_s=$('#ms2side__sx');
        var options_s =multiselect_s.find('option:selected');
        multiselect_s.live('click',function(){
          var clickedOption =findClickedOption($(this), options_s);
          options_s = $(this).find('option:selected');
          doSomething(clickedOption);//call your own function here
        });
    
        function findClickedOption(selectbox, optionsArr){
          var selectedOptions=selectbox.find('option:selected');
          if(selectedOptions.size>1){
            return selectedOptions.not(optionsArr);        
          }else{
            return selectedOptions;
          }
        }
    

提交回复
热议问题