使用JSON

时间:2018-06-18 01:38:25

标签: jquery json caching

这是我的功能代码:

var checkboxValues = JSON.parse(localStorage.getItem('checkboxValues')) || {};

jQuery(document).ready(function() {
    jQuery("input[name='GLOVE']").click(function() {

        jQuery('input[type="radio"][data-default="1"]').each(function() {
            var key = jQuery(this).attr("name");
            var value = jQuery("input[name='" + key + "']:checked").val() 
                      ? jQuery("input[name='" + key + "']:checked").val() 
                      : null;
            if (key != "GLOVE") {
                checkboxValues[this.id] = value;
            }
        });

        localStorage.setItem("checkboxValues", JSON.stringify(checkboxValues));
        jQuery.each(checkboxValues, function(key, value) {
            if (key != "option-5b2089bd80038" || key != "cboption-5b2089bd9d11b") {
                console.log(key + " " + value);
                jQuery("#" + key).prop('checked', value);
            }
        });
    });
});


jQuery.each(checkboxValues, function(key, value) {
    if (key != "option-5b2089bd80038") {
        console.log(key + " " + value);
        jQuery("#" + key).prop('checked', value);
    }
});

值已在控制台中保存,但缓存后仍无法检查按钮。

在这部分我使用了产品构建器中的条件逻辑(wordpress),所以有两个主要按钮,正面和背面,当我点击" front"这些颜色会出现(也在后面),所以当我选择一种颜色然后去"返回"当我点击" Front"再次。在条件逻辑中,如果我点击" Front" Back的组件将被隐藏,反之亦然。

这是按钮的示例图像:

screenshot]

问题:当我缓存或刷新页面时,按钮恢复为默认颜色(紫色)。如何保持按钮(例如我点击蓝色)检查在缓存后保留?

更新

问题已经解决,但重新加载限制为两次。 (当你选择一种颜色然后重新加载它,但当你重新加载它时,它会恢复到默认状态)

window.onload = function() {
    setTimeout(function() {
        jQuery('input[type="radio"][data-default="1"]').each(function() {
            jQuery(this).removeAttr("data-default");
        });


        jQuery(function() {
            jQuery('input[type="radio"]').click(function() {
                jQuery(this).removeAttr("data-default");
                if (jQuery(this).is(':checked')) {
                    jQuery(this).attr("data-default", "1");
                }

            });
        });
    }, 18000);
};
你知道它有限的原因吗?

1 个答案:

答案 0 :(得分:1)

已经回答:

window.onload = function() {
  setTimeout(function() {
          jQuery('input[type="radio"][data-default="1"]').each(function(){
            jQuery(this).removeAttr("data-default");
            });


            jQuery(function(){
              jQuery('input[type="radio"]').click(function(){
                if (jQuery(this).is(':checked'))
                {
                jQuery(this).attr("data-default" , "1");
                }

           });
        });
    }, 18000);
};

刚删除了jQuery(this).removeAttr("data-default");

感谢您的努力!

相关问题