如何将我的代码与jquery cookie集成

如何将我的代码与jquery cookie集成,jquery,cookies,Jquery,Cookies,对不起,之前解释的问题很糟糕。我需要的是创建一个cookie,它将在页面上的某些元素上方存储一些数据,具体如下: 类为“checked”的div 该div中复选框的选中状态 我试过jquery cookie,但我不知道如何在脚本中使用它。这是我需要存储的内容(很抱歉代码太傻): 这是我需要在我的站点中使用的coockie脚本: $.cookie('the_cookie', 'the_value'); 它们如何连接 您可以在此处看到html页面: 我需要的是,任何名为“SearchResult”

对不起,之前解释的问题很糟糕。我需要的是创建一个cookie,它将在页面上的某些元素上方存储一些数据,具体如下:

  • 类为“checked”的div
  • 该div中复选框的选中状态
  • 我试过jquery cookie,但我不知道如何在脚本中使用它。这是我需要存储的内容(很抱歉代码太傻):

    这是我需要在我的站点中使用的coockie脚本:

    $.cookie('the_cookie', 'the_value');
    
    它们如何连接

    您可以在此处看到html页面:

    我需要的是,任何名为“SearchResult”的div都将根据其复选框的选中/未选中状态保存其切换的“checked”类


    谢谢

    假设我已经理解了你的问题,那么以下内容应该适合你:

    $(document).ready(function() {
        // Check for cookie on page load, and set checkbox value accordingly
        if ($.cookie('MyCookie')) {
            $('.SearchResaultBodyFullUseFulls input[type="checkbox"]').attr("checked","checked");
        }
    
        // Set cookie on change of value
        // You may consider changing this logic to set the cookie on form submission, 
        // however you've not given enough information about your form for me to show this.
        $('.SearchResaultBodyFullUseFulls input[type="checkbox"]').click(function() {
            $(this).parent().parent().parent().parent().parent().parent().toggleClass('checked');
            $.cookie('MyCookie', $(this).is(":checked"));
        });
    });
    

    如果你能证明关于你的表单的更多信息(在JSFIDLE中或通过发布一些HTML),我将更好地为你提供更准确的解决方案。

    请发布你的HTML…尽管与对parent ie$(this)的问题链接调用无关。parent().parent().parent().parent().parent().parent().parent().parent()非常脆弱,考虑使用一个合适的选择器来替代.IE $(this)。toggleClass('AudioOfPrime')。我添加了这个链接-你可以在这里看到html页面:我需要的是,任何名为“SearchResult”的div都将根据其复选框的选中/未选中状态保存其切换的“checked”类
    $(document).ready(function() {
        // Check for cookie on page load, and set checkbox value accordingly
        if ($.cookie('MyCookie')) {
            $('.SearchResaultBodyFullUseFulls input[type="checkbox"]').attr("checked","checked");
        }
    
        // Set cookie on change of value
        // You may consider changing this logic to set the cookie on form submission, 
        // however you've not given enough information about your form for me to show this.
        $('.SearchResaultBodyFullUseFulls input[type="checkbox"]').click(function() {
            $(this).parent().parent().parent().parent().parent().parent().toggleClass('checked');
            $.cookie('MyCookie', $(this).is(":checked"));
        });
    });