Click event on select option element in chrome

前端 未结 13 954
闹比i
闹比i 2020-11-22 11:38

I\'m having a problem in Chrome with the following:

var items = $(\"option\", obj);  

items.each(function(){

    $(this).click(function(){

           


        
13条回答
  •  不思量自难忘°
    2020-11-22 11:56

    Since $(this) isn't correct anymore with ES6 arrow function which don't have have the same this than function() {}, you shouldn't use $( this ) if you use ES6 syntax.
    Besides according to the official jQuery's anwser, there's a simpler way to do that what the top answer says.
    The best way to get the html of a selected option is to use

    $('#yourSelect option:selected').html();
    

    You can replace html() by text() or anything else you want (but html() was in the original question).
    Just add the event listener change, with the jQuery's shorthand method change(), to trigger your code when the selected option change.

     $ ('#yourSelect' ).change(() => {
        process($('#yourSelect option:selected').html());
      });
    

    If you just want to know the value of the option:selected (the option that the user has chosen) you can just use $('#yourSelect').val()

提交回复
热议问题