简体   繁体   中英

reset the Select2 dropdowns on Ajax Form Submit

im using jquery select2 library

http://ivaynberg.github.io/select2/

Problem i am facing, i have multiple dropdowns in my form and if i submit the form through Ajax all feilds do get reset/empty except the select2 dropdowns..

i use this for the reset

$("#block-validate")[0].reset();

and it works perfectly fine and reset/empty all the fields in the form except the select2

i know there is a way to clear the the feild of select2 using this

$("#e8").select2("val", "");

But it will only reset the value for the intended select2. i mean we can do like this

$("#e8, #e9, #e10, #e11").select2("val", "");

what i am trying to say is there any way to reset/empty all the form fields including the select2 dropdowns without specifically calling there ID's.

or even just a code to reset or empty all the dropdowns for select2 without specifically calling there ID's, just all the Select2 dropdowns go reset on page.

All elements that are initialized with select2 get the class select2-offscreen .

So, to avoid resetting all select2 elements with their IDs you could do the following

jQuery('.select2-offscreen').select2('val', '');

This works for me. Select2 4.0.3

$('.select2').val('').trigger("change");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM