Trigger change event of dropdown

后端 未结 6 1047
逝去的感伤
逝去的感伤 2020-12-02 12:12

I want to trigger the change event of dropdown in $(document).ready using jquery.

I have a cascading dropdown for country and state in user details page. how can i s

6条回答
  •  没有蜡笔的小新
    2020-12-02 12:37

    I don't know that much JQuery but I've heard it allows to fire native events with this syntax.

    $(document).ready(function(){
    
        $('#countrylist').change(function(e){
           // Your event handler
        });
    
        // And now fire change event when the DOM is ready
        $('#countrylist').trigger('change');
    });
    

    You must declare the change event handler before calling trigger() or change() otherwise it won't be fired. Thanks for the mention @LenielMacaferi.

    More information here.

提交回复
热议问题