[英]Select2 - display selected item after refresh page
I use select2 to ( https://select2.github.io ) to bulding drop down list. 我使用select2( https://select2.github.io )来建立下拉列表。 I have problem with display selected item after refresh the page (my select is stored in DB).
刷新页面后,我无法显示所选项目(我的选择存储在DB中)。 Where excalcly have I add select or selected="selected" ?
我在哪里加了select或selected =“ selected”? I can't add this in because every user of my website selects own item in his settings.
我无法添加此内容,因为我网站的每个用户都在其设置中选择了自己的项目。
<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>
How you get the stored value from your server is up to you / stack. 如何从服务器获取存储的值取决于您/堆栈。 If you don't have a backend you can use
localStorage
to store what the user selected. 如果没有后端,则可以使用
localStorage
存储用户选择的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.