【问题标题】:Jquery Cookie Code not workingJquery Cookie 代码不起作用
【发布时间】:2013-05-02 11:49:34
【问题描述】:
<!-- Modal -->
<div id="appsmodal" class="modal hide fade" tabindex="-1" role="dialog"
aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel">Modal header</h3>
</div>
<div class="modal-body">
<p>One fine body…</p>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
<button class="btn btn-primary">Save changes</button>
</div>
</div>
<script type="text/javascript">
    $(document).ready(function() {

        if($.cookie('msg') == 0)
        {
            $('#appsmodal').modal('show');
            $.cookie('msg', 1);
        }

    });
</script>

我已经困惑了大约一个小时,但无济于事。在我用尽所有资源之前,我宁愿不提问。

【问题讨论】:

  • 这是一个 .length 的东西吗? if (!$.cookie('msg').length) 或类似的?如果未设置 Cooke 'msg',则它不会等于 0。它就不会存在。
  • .length?嗯? jquery cookie.js 和引导模式
  • 我想知道你的 if "if($.cookie('msg') == 0) 是否永远不会返回 true,因为如果你没有设置 $.cookie('msg'),它不会 == 0。它只是不存在。但是,询问 $.cookie('msg').length == 0 是否可能会返回您要查找的内容。
  • 也不起作用
  • 好的,但是首先,如果您删除 if 语句,cookie 是否设置?比如说,如果你将它设置为“foo”,而不是 1,你可以在 chrome 检查器、firefox firebug 或你有什么中查看它吗?另外,您的浏览器控制台是否显示任何错误或消息?

标签: jquery twitter-bootstrap jquery-cookie


【解决方案1】:

来自文档https://github.com/carhartl/jquery-cookie

Create session cookie:

$.cookie('the_cookie', 'the_value');
Create expiring cookie, 7 days from then:

$.cookie('the_cookie', 'the_value', { expires: 7 });
Create expiring cookie, valid across entire site:

$.cookie('the_cookie', 'the_value', { expires: 7, path: '/' });
Read cookie:

$.cookie('the_cookie'); // => "the_value"
$.cookie('not_existing'); // => undefined
Read all available cookies:

$.cookie(); // => { "the_cookie": "the_value", "...remaining": "cookies" }
Delete cookie:

打开您的浏览器控制台并检查是否有任何错误语句(例如,您是否包含 cookie 库)。此外,使用浏览器中的检查器确保 cookie 确实被设置:http://getfirebug.com/cookies

【讨论】:

  • 谢谢orolo。非常感谢
【解决方案2】:

终于!

我想通了。 Joomla 问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多