有了,我已經包括在內,你可以獲取和設置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有點痛苦。*'') –