Jquery/Javascript:本地存储插件不允许我存储索引,为什么?我做错了什么?
注意:我刚刚发现这将使它工作:$.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"))
}
});
它只是没有将其设置在本地存储中/ 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"))
}
});
It just doesn't set it in local storage/cookie, it's just empty.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑:既然您已经找出了错误以及描述它的方式,我只能假设它正在发生,因为存储插件不会存储数字。基本上 myindex 是调用返回时的一个数字
。但是,执行“”+ myindex 会将其转换为字符串。要检查理论,请尝试此
您正在尝试存储一个未在任何地方定义的变量 mystring 。您不应该存储变量 myindex 吗?
这应该有效
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
call. However doing a "" + myindex converts it into a string. To check the theory, try this
You are trying to store a variable mystring which isn't defined anywhere. Should you not be storing variable myindex instead?
This should work