mr.*_*ta1 2 cookies jquery drop-down-menu
任何人都可以帮助我,我无法在互联网上找到一个在jQuery中执行此操作的脚本.我需要记住下拉菜单的最后一个状态,以便在页面加载时已经选择了下拉选项.Cookie到期日期应为90天,或者让我在脚本中指定.由于我已经在我的页面上使用jQuery,我希望这个cookie脚本使用jquery,jquery cookie插件.先感谢您.我不是程序员,但我试试.
<body onLoad="GETcookie()">
<select onChange="SETcookie()">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
Run Code Online (Sandbox Code Playgroud)
使用我包含的插件,您可以获取并设置cookie.
你的代码看起来像这样:
//checks if the cookie has been set
if($.cookie('remember_select') != null) {
// set the option to selected that corresponds to what the cookie is set to
$('.select_class option[value="' + $.cookie('remember_select') + '"]').attr('selected', 'selected');
}
// when a new option is selected this is triggered
$('.select_class').change(function() {
// new cookie is set when the option is changed
$.cookie('remember_select', $('.select_class option:selected').val(), { expires: 90, path: '/'});
});
Run Code Online (Sandbox Code Playgroud)
这是你的select样子:
<select class="select_class">
<option value="1">Row 1</option>
<option value="2">Row 2</option>
<option value="3">Row 3</option>
</select>
Run Code Online (Sandbox Code Playgroud)
这是jsFiddle的演示:http://jsfiddle.net/RastaLulz/3HxCF/3/
这是jQuery cookie插件,你还没有它:http://pastebin.com/CBueT8LP