私は、ajaxを使用した2つの連鎖選択のシリーズを持っています。将来の訪問のために最初の選択を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');
});