jQuery Cookie插件保存选择值
jQuery Cookie plugin to save select value?
我有一系列使用ajax的两个链式选择,效果很好。 我需要能够在 cookie 中存储/保存第一个选择以供将来访问,但无法完全弄清楚在现有代码中如何/做什么?
$(function(){
var questions = $('#questions');
function refreshSelects(){
var selects = questions.find('select');
// Lets not do chosen on the first select
selects.not(":first").chosen({ disable_search_threshold: true });
// Listen for changes
selects.unbind('change').bind('change',function(){
// The selected option
var selected = $(this).find('option').eq(this.selectedIndex);
// Look up the data-connection attribute
var connection = selected.data('connection');
// Removing the li containers that follow (if any)
selected.closest('#questions li').nextAll().remove();
if(connection){
fetchSelect(connection);
}
});
}
var working = false;
function fetchSelect(val){
if(working){
return false;
}
working = true;
$.getJSON('citibank.php',{key:val},function(r){
var connection, options = '';
switch (r.type) {
case 'select':
$.each(r.items,function(k,v){
connection = '';
if(v){
connection = 'data-connection="'+v+'"';
}
options+= '<option value="'+k+'" '+connection+'>'+k+'</option>';
});
if(r.defaultText){
// The chose plugin requires that we add an empty option
// element if we want to display a "Please choose" text
options = '<option></option>'+options;
}
// Building the markup for the select section
$('<li>'
<p>'+r.title+'</p>'
<select data-placeholder="'+r.defaultText+'">'
'+ options +''
</select>'
<span class="divider"></span>'
</li>').appendTo(questions);
refreshSelects();
break;
case 'html':
$(r.html).appendTo(questions);
break;
}
working = false;
});
}
$('#preloader').ajaxStart(function(){
$(this).show();
}).ajaxStop(function(){
$(this).hide();
});
// Initially load the product select
fetchSelect('callTypeSelect');
});
这是一篇关于使用 jCookies
的好文章。
http://tympanus.net/codrops/2011/09/04/j-is-for-jcookies-http-cookie-handling-for-jquery/
设置它的代码是这样的:
$.jCookies({
name : 'Person',
value : { first: 'John', last: 'Smith', Age: 25 }
});
获取饼干是这样的
var person = $.jCookies({ get : 'Person' });
服务器上是否需要该值?如果你不这样做,我会重新评论jStorage插件。它使用本地存储和用户数据来保存信息。这样做的好处是,这些值不会像 cookie 那样在每次请求时发送到您的服务器。用法非常简单:
$.jStorage.set("something", {data: [1,2,3], other: "a string"});
和
$.jStorage.get("something"); // returns {data: [1,2,3], other: "a string"}
在您的代码中,它将类似于:
$(function() {
var questions = $('#questions');
var lastSelection = $.jStorage.get("lastSelection");
if(lastSelection) {
questions.find("select:first").val(lastSelection);
}
// more code....
selects.unbind('change').bind('change',function(){
var selected = $(this).find('option').eq(this.selectedIndex);
if(questions.find("select:first")[0] === this) { // Only save if it's the first combo (you could change this to a better way to identify the first select)
$.jStorage.set("lastSelection", selected);
}
// more code....
});
相关文章:
- 如何使用javascript localStorage保存用户选择并在不同的html页面中显示
- 当与Dojo验证一起使用时,Chrome 41密码保存会做出错误的选择
- jQuery日期[时间]选择器-如何保存时间范围(+修改日期格式)
- 如何强制用户从下拉列表和按钮中选择一个值以启用保存按钮
- jQuery UI 选项卡取消选择事件或在选择时保存上一个选项卡
- 表单选择不保存在本地存储中
- Angular js,在选择元素上,我想 POST 数据以将其保存在数据库中,然后我想使用 PUT 更新它而无需重新加载
- 保存当前选择的<李>在页面刷新后的Angular中
- 数据表复制,只保存单个多重筛选选择的选定行
- 一旦使用了jQuery日期选择器,无法使“保存更改”按钮不灰
- 存储日期选择器日期并保存在数据库中
- 将查询选择器保存在要追加的变量中不起作用
- jQuery Cookie插件保存选择值
- 保存选项卡选择
- 在下拉列表中显示保存的数据 - html 选择标签
- 日期未通过使用 jQuery 日期选择器轨道保存在数据库中
- 让用户能够选择保存文件的位置
- 选择保存镜像的目录
- 将选择/保存的值(从数据库)设置/显示到一个选择框中,以便进行编辑
- 从服务器下载文件:打开文件对话框选择保存文件的文件夹