Jquery cookie记住下拉菜单的最后状态

任何人都可以帮助我,我无法在互联网上找到一个在jQuery中执行此操作的脚本。 我需要记住下拉菜单的最后一个状态,以便在页面加载时已经选择了下拉选项。 Cookie到期日期应为90天,或者让我在脚本中指定。 由于我已经在我的页面上使用jQuery,我希望这个cookie脚本使用jquery,jquery cookie插件。 先感谢您。 我不是程序员,但我试试。

  1 2 3  

使用我包含的插件,您可以获取并设置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: '/'}); }); 

以下是您select

  

这是jsFiddle的演示: http : //jsfiddle.net/RastaLulz/3HxCF/3/

这是jQuery cookie插件,你还没有它: http : //pastebin.com/CBueT8LP

首先不要做body onLoad(应该是小写的onload),但在我的例子中你不需要你的onload和onchange但是:

 // This replace onload $(function() { // Read your cookie and other things up to you to do... $.cookie('the_cookie'); // on change of your select $('select').on('change', function() { // Set the cookie $.cookie('the_cookie', 'the_value', { expires: 90, path: '/'}); }); }); 

PS:我的脚本是jQuery 1.7(对于on)所以看看你是否是最新的!

编辑:设置90天到期