![](/img/trans.png)
[英]select2 after refresh cannot be auto selected,initSelection and callback no working
[英]Select2 - display selected item after refresh page
我使用select2( https://select2.github.io )來建立下拉列表。 刷新頁面后,我無法顯示所選項目(我的選擇存儲在DB中)。 我在哪里加了select或selected =“ selected”? 我無法添加此內容,因為我網站的每個用戶都在其設置中選擇了自己的項目。
<script>
$(document).ready(function() {
$(".js-example-basic-single").select2({
});
$("select").select2({
});
});
</script>
<select class="js-example-basic-single" name="field">
<option value="city1">city1</option>
<option value="city2">city2</option>
</select>
<script>
$(document).ready(function() {
var storedValue = 'city2';
var $select = $(".js-example-basic-single").select2({});
$select.select2('val', 'storedValue');
});
</script>
<select class="js-example-basic-single" name="field">
<option value="city1">city1</option>
<option value="city2">city2</option>
</select>
如何從服務器獲取存儲的值取決於您/堆棧。 如果沒有后端,則可以使用localStorage
存儲用戶選擇的內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.