jquery cookie 使用以前的值

发布于 2024-11-29 07:51:08 字数 4592 浏览 1 评论 0原文

这是我的手风琴的生成代码:

<div id="treeview-accordion">
<h3><a href="#" accindex="0">Basic</a></h3>
<div>
    <ul class="navigation-treeview treeview-sanjo" id="yw0">
        <li><span style="font-weight:bold"><a href="/sanjo/site/index"> Home</a></span>
            <ul>
                <li><span> Profile</span>
                    <ul>
                        <li><span><a href="/sanjo/user/profile"> View Profile</a></span></li>
                        <li><span><a href="/sanjo/user/profile/edit"> Update Profile</a></span></li>
                        <li><span><a href="/sanjo/user/profile/changepassword"> Change Password</a></span></li>
                    </ul>
                </li>
            </ul>
        </li>
        <li><span> Personnel Management</span>
            <ul>
                <li><span><a href="/sanjo/user/admin"> Manage Personnel</a></span>
                <ul>
                    <li><span><a href="/sanjo/user"> List Personnel</a></span></li>
                    <li><span> Add Personnel</a></span></li>
                </ul>
                </li>
            </ul>
        </li>
    </ul>
</div>
<h3><a href="#" accindex="1">Basic2</a></h3>
<div>
    <ul class="navigation-treeview treeview-sanjo" id="yw01">
        <li><span style="font-weight:bold"><a href="/sanjo/site/index2"> Home2</a></span>
            <ul>
                <li><span> Profile</span>
                    <ul>
                        <li><span><a href="/sanjo/user/profile2"> View Profile2</a></span></li>
                        <li><span><a href="/sanjo/user/profile/edit2"> Update Profile2</a></span></li>
                        <li><span><a href="/sanjo/user/profile/changepassword2"> Change Password2</a></span></li>
                    </ul>
                </li>
            </ul>
        </li>
        <li><span> Personnel Management2</span>
            <ul>
                <li><span><a href="/sanjo/user/admin2"> Manage Personnel2</a></span>
                <ul>
                    <li><span><a href="/sanjo/user2"> List Personnel2</a></span></li>
                    <li><span> Add Personnel2</a></span></li>
                </ul>
                </li>
            </ul>
        </li>
    </ul>
</div>

每次单击链接时,我都会根据链接所在的

在 cookie“sanjotreeaccordion”中保存一个值:

$("#tree-accordion a").bind("click",function(){
                    var cookie=$(this).closest("div").prev("h3").find("a").attr("accindex");
                    $.cookie("sanjotreeaccordion", cookie); 
                    alert($.cookie("sanjotreeaccordion"));
                   });

然后在页面上重新加载后,我获得了 cookie“sanjotreeaccordion”值,因此我可以使用它来确定要打开哪个折叠窗格:

$(document).ready(function(){
    alert($.cookie('sanjotreeaccordion'));
    var ind = ($.cookie('sanjotreeaccordion') || 0)*1;
    $('#tree-accordion').wijaccordion({'autoHeight':false,'requireOpenedPane':false,'selectedIndex':ind});
    $.cookie('sanjotreeaccordion',ind);    
});

您可能已经注意到我有警报,并且我使用它来确定 cookie 的值。 WijAccordion 是创建手风琴的函数,它将根据其 selectedIndex 选项的值打开窗格(0 表示第一个窗格,1 表示第 2 次,以此类推)。

现在是奇怪的部分(我正在打字的下一部分)。

最初,当我运行代码时,cookie 尚不存在,因此它将打开第一个窗格(cookie 为空,因此它返回 0 然后乘以 1)。然后该值将被保存到 cookie 中。 (我检查了cookie,它是0)。

然后我单击第二个窗格上的链接,因此我希望它将 1 保存在 cookie 中(警报表示 cookie 的值为 1)。但是当页面重新加载时,cookie值的alert仍然是0。当我再次单击任何链接时,cookie 值将更改为 1。换句话说,当页面重新加载时,我得到了 cookie 的先前值。

我的代码哪里有问题?还是我的逻辑不正确?我是否没有遵循我想要遵循的顺序,即

  1. 单击链接
  2. 获取其上部

    的“accindex”

  3. 将值保存到 cookie
  4. 页面重新加载
  5. 的值
  6. 获取 cookie使用 打开手风琴的相应窗格的 cookie 值?

This is the generated code of my accordion:

<div id="treeview-accordion">
<h3><a href="#" accindex="0">Basic</a></h3>
<div>
    <ul class="navigation-treeview treeview-sanjo" id="yw0">
        <li><span style="font-weight:bold"><a href="/sanjo/site/index"> Home</a></span>
            <ul>
                <li><span> Profile</span>
                    <ul>
                        <li><span><a href="/sanjo/user/profile"> View Profile</a></span></li>
                        <li><span><a href="/sanjo/user/profile/edit"> Update Profile</a></span></li>
                        <li><span><a href="/sanjo/user/profile/changepassword"> Change Password</a></span></li>
                    </ul>
                </li>
            </ul>
        </li>
        <li><span> Personnel Management</span>
            <ul>
                <li><span><a href="/sanjo/user/admin"> Manage Personnel</a></span>
                <ul>
                    <li><span><a href="/sanjo/user"> List Personnel</a></span></li>
                    <li><span> Add Personnel</a></span></li>
                </ul>
                </li>
            </ul>
        </li>
    </ul>
</div>
<h3><a href="#" accindex="1">Basic2</a></h3>
<div>
    <ul class="navigation-treeview treeview-sanjo" id="yw01">
        <li><span style="font-weight:bold"><a href="/sanjo/site/index2"> Home2</a></span>
            <ul>
                <li><span> Profile</span>
                    <ul>
                        <li><span><a href="/sanjo/user/profile2"> View Profile2</a></span></li>
                        <li><span><a href="/sanjo/user/profile/edit2"> Update Profile2</a></span></li>
                        <li><span><a href="/sanjo/user/profile/changepassword2"> Change Password2</a></span></li>
                    </ul>
                </li>
            </ul>
        </li>
        <li><span> Personnel Management2</span>
            <ul>
                <li><span><a href="/sanjo/user/admin2"> Manage Personnel2</a></span>
                <ul>
                    <li><span><a href="/sanjo/user2"> List Personnel2</a></span></li>
                    <li><span> Add Personnel2</a></span></li>
                </ul>
                </li>
            </ul>
        </li>
    </ul>
</div>

And everytime I click on a link, I am saving a value in the cookie "sanjotreeaccordion" based on which <h3> the link is under:

$("#tree-accordion a").bind("click",function(){
                    var cookie=$(this).closest("div").prev("h3").find("a").attr("accindex");
                    $.cookie("sanjotreeaccordion", cookie); 
                    alert($.cookie("sanjotreeaccordion"));
                   });

Then on page reload, I get the cookie "sanjotreeaccordion" value, so I can use it to determine which accordion pane to open:

$(document).ready(function(){
    alert($.cookie('sanjotreeaccordion'));
    var ind = ($.cookie('sanjotreeaccordion') || 0)*1;
    $('#tree-accordion').wijaccordion({'autoHeight':false,'requireOpenedPane':false,'selectedIndex':ind});
    $.cookie('sanjotreeaccordion',ind);    
});

You might have noticed that I have there alerts, and I use it to determine the value of the cookie. WijAccordion is the function creating the accordion, and will open the pane depending on the value of its selectedIndex option (0 means first pane,1 means 2nd,and so on).

Now the wierd part (I am doing the next part as I type).

Initially, when I ran my codes, the cookie does not exist yet so it will open the first pane (cookie is null,so it returns 0 then multiplied to 1). And then that value will be saved to the cookie. (I checked the cookie,it is 0).

Then I clicked on a link on the second pane, so I expect it to save 1 in the cookie (the alert said that the value of the cookie is 1). But when the page reloaded, the alert of the cookie value is still 0. When I clicked on any link again, that will be time when the cookie value is changed to 1. In other words,I am getting the supposedly previous value of the cookie when page reloads.

Where is the problem with my codes?or is it my logic that is incorrect? Am I not following the sequence that I want to follow, which is

  1. click a link
  2. get "accindex" of its upper <h3>
  3. save the value to the cookie
  4. page reloads
  5. get the value of the cookie
  6. use the cookie value to open the appropriate pane of the accordion?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

世态炎凉 2024-12-06 07:51:08

有几件事供您查看:

1)我不知道这是否只是您问题中的拼写错误,但您的容器 div 实际上具有 id“treeview-accordion”,但是您正在绑定点击事件到 ID 为“tree-accordian”的元素内的锚点。所以这可以解释为什么此时 cookie 没有被正确设置。

2) 在使用已设置的索引初始化每个页面上的手风琴后,您还设置了一个 cookie。所以这“可能”是您看到奇怪行为的原因。

这就是我开始寻找解决方案的地方。我无法访问可以使用您现在正在使用的插件完全检查它的环境,但如果这不能引导您走向正确的方向,我将很乐意更详细地查看。

Couple of things for you to look at:

1) I don't know if this is just a typo in your question or not, but your container div actually has the id "treeview-accordion" and yet, you're binding the click event to anchors within an element which has the id "tree-accordian". So this could explain why the cookie is not being set correctly at this point.

2) You're also setting a cookie after you initialise the accordian on each page using the index that's been set. So this 'could' be the reason why you are seeing the strange behaviour.

That would be where I would start looking for the solution. I don't have access to an environment where I can check it out fully with the plugins you're using right now, but if this doesn't guide you in the right direction, will be happy to look at in more detail.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文