JQuery select2 set default value from an option in list?

前端 未结 10 797
傲寒
傲寒 2020-12-04 13:07

I want to be able to set the default/selected value of a select element using the JQuery Select2 plugin.

10条回答
  •  孤街浪徒
    2020-12-04 13:41

    The above solutions did not work for me, but this code from Select2's own website did:

    $('select').val('US'); // Select the option with a value of 'US'
    $('select').trigger('change'); // Notify any JS components that the value changed
    

    Webpage found here

    Hope this helps for anyone who is struggling, like I was.

提交回复
热议问题