Get clicked option in multiple dropdown

前端 未结 3 989
离开以前
离开以前 2020-12-18 00:52

I have a multi select dropdown eg:


                        
    
提交评论

  • 2020-12-18 01:32

    You can get it in the click handler for each option element:

    $("#myList option").click(function() {
        var clickedOption = $(this);
    });
    

    Update

    EDIT: As I manipulate the list inside a change event, I can't do it in a click event.

    In that case you need to delegate the event using on. Try this:

    $("#myList").on("click", "option", function() {
        var clickedOption = $(this);
    });
    

    One thing to note, however, is that option elements will not raise click events at all in IE, so neither of the above will not work in that browser.

    0 讨论(0)
  • 2020-12-18 01:35

    Would something like the following help you?

    $('#myList').delegate('option', 'click', function (opt) {
      alert('Option ' + opt.value + ' was clicked');
    });
    
    0 讨论(0)
  • 提交回复
    热议问题