开发者

Jquery/Javascript: Local storage plugin wont allow me to store an index, why? what am i doing wrong?

开发者 https://www.devze.com 2023-02-17 03:29 出处:网络
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: $.S

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.


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"))  
    }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消