刷新后保留选定选项(表格/选择) [英] Keep selected option (form/select) after refresh

查看:30
本文介绍了刷新后保留选定选项(表格/选择)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复项:
Html select option lost data after submit

我有一个选择菜单,它应该在页面刷新后保留所选选项。这是一个例子:

<select id="form_frame" name="frame" onchange="getData(this);"/>
   <option value="data1" selected="selected">Data 1</option>
   <option value="data2">Data 2</option>
</select>

函数getData只是将信息拉取给用户。

我正在对动态内容使用Smarty/php。

接受建议,谢谢!

推荐答案

如何处理本地存储:

$(function() {
    if (localStorage.getItem('form_frame')) {
        $("#form_frame option").eq(localStorage.getItem('form_frame')).prop('selected', true);
    }

    $("#form_frame").on('change', function() {
        localStorage.setItem('form_frame', $('option:selected', this).index());
    });
});

FIDDLE

这篇关于刷新后保留选定选项(表格/选择)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆