Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php jQuery Cookie插件保存选择值?_Php_Javascript_Jquery_Json - Fatal编程技术网

Php jQuery Cookie插件保存选择值?

Php jQuery Cookie插件保存选择值?,php,javascript,jquery,json,Php,Javascript,Jquery,Json,我有一系列使用ajax的两个链式选择,效果非常好。我需要能够在cookie中存储/保存第一个select,以备将来访问,但无法完全理解在现有代码中如何/做什么 $(function(){ var questions = $('#questions'); function refreshSelects(){ var selects = questions.find('select'); // Lets not do chosen on the

我有一系列使用ajax的两个链式选择,效果非常好。我需要能够在cookie中存储/保存第一个select,以备将来访问,但无法完全理解在现有代码中如何/做什么

$(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');
});
$(函数(){
变量问题=$(“#问题”);
函数refreshSelects(){
var selects=questions.find('select');
//让我们不要在第一次选择时选择
selects.not(“:first”).selected({disable\u search\u threshold:true});
//倾听变化
选择.unbind('change')。bind('change',function(){
//所选选项
所选变量=$(this.find('option').eq(this.selectedIndex);
//查找数据连接属性
var connection=selected.data('connection');
//卸下下面的li容器(如果有)
选中。最近(“#问题li”).nextAll().remove();
如果(连接){
获取选择(连接);
}
});
}
var=false;
函数fetchSelect(val){
如果(工作){
返回false;
}
工作=真实;
$.getJSON('citibank.php',{key:val},函数(r){
变量连接,选项=“”;
开关(r型){
案例“选择”:
$。每个(r.项目,功能(k,v){
连接=“”;
如果(v){
连接='数据连接='+v+'';
}
选项+=''+k+'';
});
if(r.defaultText){
//选择的插件要求我们添加一个空选项
//元素,如果要显示“请选择”文本
选项=“”+选项;
}
//为“选择”节生成标记
$('
  • \ “+r.title+”

    \ \ “+选项+”\ \ \ 附件(问题); 刷新选择(); 打破 案例“html”: $(r.html)。附录(问题); 打破 } 工作=假; }); } $('#preload').ajaxStart(函数(){ $(this.show(); }).ajaxStop(函数(){ $(this.hide(); }); //最初加载产品选择 fetchSelect('callTypeSelect'); });
    这是一篇关于使用
    jCookies
    的好文章

    设置它的代码如下所示:

    $.jCookies({
        name : 'Person',
        value : { first: 'John', last: 'Smith', Age: 25 }
    });
    
    得到饼干的过程是这样的

    var person = $.jCookies({ get : 'Person' });
    

    这是一篇关于使用jCookies的好文章

    设置它的代码如下所示:

    $.jCookies({
        name : 'Person',
        value : { first: 'John', last: 'Smith', Age: 25 }
    });
    
    得到饼干的过程是这样的

    var person = $.jCookies({ get : 'Person' });
    

    服务器上是否需要该值?如果你不这样做,我建议你。它使用本地存储和用户数据来保存信息。这有着和饼干一样的好处。 用法非常简单:

    $.jStorage.set("something", {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....
    });
    

    服务器上是否需要该值?如果你不这样做,我建议你。它使用本地存储和用户数据来保存信息。这有着和饼干一样的好处。 用法非常简单:

    $.jStorage.set("something", {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....
    });
    

    …不需要存储在服务器上。我如何将您的答案准确地放入现有代码中?我对这个很陌生,希望通过观看来学习。我不知道你想保存什么价值。。。它是更改事件的“选定”变量吗?是的,我想保存第一个选择框更改事件中的选定值。谢谢…不需要存储在服务器上。我如何将您的答案准确地放入现有代码中?我对这个很陌生,希望通过观看来学习。我不知道你想保存什么价值。。。它是更改事件的“选定”变量吗?是的,我想保存第一个选择框更改事件中的选定值。谢谢。你能根据我提供的代码片段给我一个更具体的例子吗?如果你想要一个具体的例子,你的代码会更具体一些。我明白你的意思,但你没有真正解释清楚。此外,理论上我会立即检查一块饼干,如果它不存在,那么就继续做,就像它是一个新的选择一样。当用户做出选择时,然后创建一个存储所需信息的cookie。我提供的教程为您提供了大量信息。您能根据我提供的代码片段为我提供一个更具体的示例吗?如果您想要一个具体的示例,您的代码将更具体一些。我明白你的意思,但你没有真正解释清楚。此外,理论上我会立即检查一块饼干,如果它不存在,那么就继续做,就像它是一个新的选择一样。当用户做出选择时,然后创建一个存储所需信息的cookie。我提供的教程为您提供了大量信息。