select2를 이용하여 셀렉 박스를 만들고, 특정 버튼 클릭시 선택항목 초기화를 하고 싶었다. Show 그러나, value를 찍어보면 초기화가 된 것으로 보이나, 화면에는 여전히 첫번째 element가 선택되어 있었다. 해결방법을 찾아서 기록한다.
저작자표시비영리변경금지 How do I set the placeholder on value reset by select2. In my example If locations or grade select boxes are clicked and my select2 has a value than the value of select2 should reset and show the default placeholder. This script is resetting the value but won't show the placeholder Creating new options in the dropdownNew options can be added to a Select2 control programmatically by creating a new Javascript
The third parameter of Create if not existsYou can use
Selecting optionsTo programmatically select an option/item for a Select2 control, use the jQuery
You can also pass an array to
Select2 will listen for the Preselecting options in an remotely-sourced (AJAX) Select2For Select2 controls that receive their data from an AJAX source, using The best way to deal with this, therefore, is to simply add the preselected item as a new option. For remotely sourced data, this will probably involve creating a new API endpoint in your server-side application that can retrieve individual items:
Notice
that we manually trigger the Clearing selectionsYou may clear all current selections in a Select2 control by setting the value of the control to |