Jquery/Javascript:本地存储插件不允许我存储索引,为什么?我做错了什么?

发布于 2024-10-21 19:19:56 字数 1346 浏览 2 评论 0原文

注意:我刚刚发现这将使它工作:$.Storage.set("whichp", ""+myindex),添加引号,然后加号,然后是变量。为什么它不能仅使用这样的变量:$.Storage.set("whichp", myindex)?如果我只是删除引号并保留加号,如下所示: $.Storage.set("whichp", myindex) 它也不会工作。

我正在使用存储本地数据的 Jquery 插件,如果你的浏览器有这个能力。如果没有,它会存储一个 cookie。他在开发者网站上说:

名称和值应该是字符串。 某些浏览器可能接受非字符串 价值观,但并非所有人都这样做。

设置数据的格式是:

$.Storage.set("name", "value") 

并获取值:

$.Storage.get("name") 

我想我可以设置一个变量,然后插入该 var 作为值,如下所示:

var myindex = $(this).index()
$.Storage.set("whichp",  mystring)

但它不起作用...如果我这样做:

var mynote = "this is a test"
$.Storage.set("whichp",  mynote)

那么它会工作....我不明白为什么它不允许我将索引放入存储中...

此点击事件的完整代码如下。

$('P').click(function () {
        var myindex = $(this).index()
    if (!$.Storage.get("whichp")) {
        $.Storage.set("whichp",  myindex)           
    } else {
        alert($.Storage.get("whichp"))  
    }
});

Jquery 插件

它只是没有将其设置在本地存储中/ cookie,它只是空的。

NOTE: I just figured out that this will make it work: $.Storage.set("whichp", ""+myindex), adding the quotes, then a plus, then the variable. why wont it work with just the variable like this: $.Storage.set("whichp", myindex)? If I even just remove the quotes and leave the plus, like so: $.Storage.set("whichp", myindex) it wont work either.

I'm using a Jquery plugin that stores local data, if your browser has the capability. If not, it stores a cookie instead. On the developers site he says:

Names and values should be strings.
Some browsers may accept non-string
values, but not all do.

The format to set data is:

$.Storage.set("name", "value") 

and to get the value:

$.Storage.get("name") 

I thought I could set a variable, then insert that var as the value, like this:

var myindex = $(this).index()
$.Storage.set("whichp",  mystring)

but it's not working... if I were to do this though:

var mynote = "this is a test"
$.Storage.set("whichp",  mynote)

then it will work.... I don't understand why it wont allow me to put the index into storage...

my full code for this click event is below.

$('P').click(function () {
        var myindex = $(this).index()
    if (!$.Storage.get("whichp")) {
        $.Storage.set("whichp",  myindex)           
    } else {
        alert($.Storage.get("whichp"))  
    }
});

Jquery plugin

It just doesn't set it in local storage/cookie, it's just empty.

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

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

发布评论

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

评论(1

朕就是辣么酷 2024-10-28 19:19:56

编辑:既然您已经找出了错误以及描述它的方式,我只能假设它正在发生,因为存储插件不会存储数字。基本上 myindex 是调用返回时的一个数字

$(this).index()

。但是,执行“”+ myindex 会将其转换为字符串。要检查理论,请尝试此

 $.Storage.set("whichp",  myindex.toString()) 

您正在尝试存储一个未在任何地方定义的变量 mystring 。您不应该存储变量 myindex 吗?

这应该有效

$('P').click(function () {
        var myindex = $(this).index()
    if (!$.Storage.get("whichp")) {
        $.Storage.set("whichp",  myindex)          
    } else {
        alert($.Storage.get("whichp"))  
    }
});

EDIT: Since you have figured out the error and the way you have described it, I can only assume that it is happening because the Storage plugin won't store a number. basically myindex is a number when it is returned by the

$(this).index()

call. However doing a "" + myindex converts it into a string. To check the theory, try this

 $.Storage.set("whichp",  myindex.toString()) 

You are trying to store a variable mystring which isn't defined anywhere. Should you not be storing variable myindex instead?

This should work

$('P').click(function () {
        var myindex = $(this).index()
    if (!$.Storage.get("whichp")) {
        $.Storage.set("whichp",  myindex)          
    } else {
        alert($.Storage.get("whichp"))  
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文