Jquery event when user makes selection in dropdown

前端 未结 4 546
北荒
北荒 2020-12-29 12:17

I have a drop down list. In Jquery what is the event I would use when the user makes a selection.

The id of the dropdown is drp1

I tried the following but di

4条回答
  •  天命终不由人
    2020-12-29 12:57

    Use the change() event:

    $("#ddrp1").change(function() {
        // Pure JS
        var selectedVal = this.value;
        var selectedText = this.options[this.selectedIndex].text;
    
        // jQuery
        var selectedVal = $(this).find(':selected').val();
        var selectedText = $(this).find(':selected').text();
    });
    

    In jQuery 1.7, you can use .on()

    $("#ddrp1").on("change", function() {
        // Pure JS
        var selectedVal = this.value;
        var selectedText = this.options[this.selectedIndex].text;
    
        // jQuery
        var selectedVal = $(this).find(':selected').val();
        var selectedText = $(this).find(':selected').text();
    }​​​​);​
    

    Here's a working jsFiddle using on()

提交回复
热议问题