1

我有一系列使用 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');
});
4

2 回答 2

3

这是关于使用的好文章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 }
});

获取 cookie 是这样的

var person = $.jCookies({ get : 'Person' });
于 2012-06-28T17:32:14.590 回答
1

你需要服务器上的那个值吗?如果你不这样做,我推荐jStorage 插件。它使用本地存储和 userData 来保存信息。这样做的好处是,不会像 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....
});
于 2012-06-28T17:38:43.330 回答