从JQuery cookie添加和读取所有数组

时间:2016-01-18 17:26:28

标签: jquery arrays cookies arraylist add

我试图检查一个简单的html页面,我可以添加数据,因为我将第一个按钮按入jquery cookie数组,而另一个按钮可以读取列表中的所有cookie值。

只是尝试添加到cookie数组并将其值打印到span中。但只是这个代码不会抛出任何控制台错误。和数组大小警报始终为零。我的js代码有什么问题?你能帮忙吗?

    <html>
    <head>
        <script src="Scripts/jquery-2.2.0.min.js"></script>
        <script src="Scripts/jquery.cookie.js"></script>

    </head>
    <body>
        <form id="form1">
            <div>

                <input id="Text1" type="text" />

                <input id="Button1" type="button" value="ADD TO COOKIE ARRAY" onclick="addToCookie();" />
            </div>
        </form>
        <p>
            <input id="Button2" type="button" value="READ ALL COOKIE ARRAY INTO SPAN" onclick="readAllFromCookie();" />
            <span id="span_listhere"></span>
        </p>

        <script type="text/javascript">

            ///////////////////////
            $.cookie.defaults.path = '/';
            var cookieList = function (cookieName) {

                var cookie = $.cookie(cookieName);
                var items = cookie ? cookie.split(/,/) : new Array();

                return {
                    "add": function (val) {
                        var index = items.indexOf(val);
                        if (index == -1) {
                            items.push(val);
                            $.cookie(cookieName, items.join(','));
                        }
                    },
                    "remove": function (val) {
                        indx = items.indexOf(val);
                        if (indx != -1) items.splice(indx, 1);
                        $.cookie(cookieName, items.join(','));
                    },
                    "clear": function () {
                        items = null;
                        $.cookie(cookieName, null);
                    },
                    "items": function () {
                        return items;
                    }
                }
            }
            ///////////////////////
             list_stockno = new cookieList("c_stockno");

            ///////////////////////
            function addToCookie() {
                alert("addToCookie has been started");
                $stockno = $('#Text1').val();

                list_stockno.add($stockno);

                alert(list_stockno.items.length);
            }
            ///////////////////////
            function readAllFromCookie() {
                alert("readAllFromCookie has been started");

                for ($i = 0; $i < list_stockno.items.length; $i++) {
                    $sn = list_stockno[$i];

                    $mydata = ("stockno:" + $sn);
                    $('#span_listhere').append($mydata);
                }


            }

        </script>
    </body>
    </html>

修改如回复中所述。但没有运气

这不是答案。刚刚添加了路径参数名称/值对。如下所述,没有运气。 仍然数组值警报为零,没有任何反应。并且在按下两个按钮元素时也没有出现任何控制台错误。

<html>
<head>
    <script src="Scripts/jquery-2.2.0.min.js"></script>
    <script src="Scripts/jquery.cookie.js"></script>

</head>
<body>
    <form id="form1">
        <div>

            <input id="Text1" type="text" />

            <input id="Button1" type="button" value="ADD TO COOKIE ARRAY" onclick="addToCookie();" />
        </div>
    </form>
    <p>
        <input id="Button2" type="button" value="READ ALL COOKIE ARRAY INTO SPAN" onclick="readAllFromCookie();" />
        <span id="span_listhere"></span>
    </p>

    <script type="text/javascript">

        ///////////////////////

        var cookieList = function (cookieName) {

            var cookie = $.cookie(cookieName, { path: '/' });
            var items = cookie ? cookie.split(/,/) : new Array();

            return {
                "add": function (val) {
                    var index = items.indexOf(val);
                    if (index == -1) {
                        items.push(val);
                        // $.cookie(cookieName, items.join(','));
                        $.cookie(cookieName, items.join(','), { path: '/' });
                    }
                },
                "remove": function (val) {
                    indx = items.indexOf(val);
                    if (indx != -1) items.splice(indx, 1);
                    $.cookie(cookieName, items.join(','), { path: '/' });
                },
                "clear": function () {
                    items = null;
                    $.cookie(cookieName, { path: '/' });
                },
                "items": function () {
                    return items;
                }
            }
        }
        ///////////////////////
         list_stockno = new cookieList("c_stockno");

        ///////////////////////
        function addToCookie() {
            alert("addToCookie has been started");
            $stockno = $('#Text1').val();

            list_stockno.add($stockno);

            alert(list_stockno.items.length);
        }
        ///////////////////////
        function readAllFromCookie() {
            alert("readAllFromCookie has been started");

            for ($i = 0; $i < list_stockno.items.length; $i++) {
                $sn = list_stockno[$i];

                $mydata = ("stockno:" + $sn);
                $('#span_listhere').append($mydata);
            }


        }

    </script>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

可能不是那样,但只是为了确定,您是否尝试过指定Cookie的路径?

$.cookie(cookieName, items.join(','), { path: '/YourAppName' });