简体   繁体   中英

How do I show the selected value of select2?

I am trying to set the value dynamically to my combobox on which I have applied select2 jQuery class. After applying code it sets the value to that combobox; but it still says "Select one". Can anyone help me to show the selected value? Please.

HTML :

<select id="cmboDemo">
 <option value="">Select one</option>
 <option value="Bob">Bob</option>
 <option value="Scott">Scott</option>
</select>

Javascript :

$("#cmboDemo").select2();
//....
$("#cmboDemo").val("Scott");
...//    

In result scott is selected but combobox shows " Select one ".

Remove $("#cmboDemo").select2();

and use the second one only:

$("#cmboDemo").val("Scott");

By using select2.js some additional layers of css are overlapped actual combo-box. By using all above code the value is set to select box; but because of that upper style layer it shows first value. ie Select one. So by changing that upper layer's text will fixed the problem.

This is the final outcome:

$("#select2-cmboDemo-container").text("Scott");

Thanks.

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