有了,我已经包括在内,你可以获取和设置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
会是什么样子:
<select class="select_class">
<option value="1">Row 1</option>
<option value="2">Row 2</option>
<option value="3">Row 3</option>
</select>
下面是它的jsfiddle演示:http://jsfiddle.net/RastaLulz/3HxCF/3/
这里是jQuery cookie插件,因为你还没有它:http://pastebin.com/CBueT8LP
* Ahem。*您不需要jQuery来管理cookie。只需找到* Javascript *安装程序即可为您管理它。 '' –
并阅读本文,最后有一些有用的功能:http://www.quirksmode.org/js/cookies.html –
(*虽然我确实希望jQuery库包含'.cookie ()'函数的工作方式就像'$ .data()'一样工作,因为在Javascript的对接中cookie有点痛苦。*'') –