简体   繁体   English

无法使sessionStorage正常工作

[英]Can't get sessionStorage to work correctly

I have multiple buttons that when they are clicked an image is loaded and the image is supposed to stay there based even when the page refreshes. 我有多个按钮,单击它们时会加载图像,并且即使页面刷新,图像也应该停留在该位置。 When I use this the button with the highest setItem value always shows even if I click on other button. 当我使用此按钮时,即使单击其他按钮,也会始终显示具有最高setItem值的按钮。 How do I fix this? 我该如何解决?

here is one of the scripts: 这是脚本之一:

<script type="text/javascript">
            var isImage1 = sessionStorage.getItem('2');

            function showImage1() {
            sessionStorage.setItem('isImage1', '2');
            $("#loadingImage1").show();
            $("#loadingImage").hide();
            $("#loadingImage2").hide();
            $("#loadingImage3").hide();
            $("#loadingImage4").hide();
            $("#loadingImage5").hide();
            $("#loadingImage6").hide();

            }
            if(isImage1 == 2) showImage1();

        </script>

and here is one of my buttons: 这是我的按钮之一:

 <input name="EPL/MECH DESIGN - TECHS" style="white-space:normal"  
   onclick="moveText(this.name);showImage1();form1.submit()" 
 style="width: 275px"  type="button" value="7SBD EPL/Mech. Design Techs" />

Update: I have updated this line 更新:我已经更新了这一行

 var isImage1 = sessionStorage.getItem('2');

to

   var isImage1 = sessionStorage.getItem('isIamge1');

but my issue still exists, that the isImage with the largest value stays even when i click the other buttons, so help is still needed. 但是我的问题仍然存在,即使当我单击其他按钮时,具有最大值的isImage仍然存在,因此仍然需要帮助。

In your session storage, you are setting the value of the 'isImage1' Item to '2' 在会话存储中,将“ isImage1”项的值设置为“ 2”

sessionStorage.setItem('isImage1', '2');

But in your code to retrieve the value you are actually retrieving the item '2' 但是在获取值的代码中,您实际上是在检索项目“ 2”

var isImage1 = sessionStorage.getItem('2');

You need to change your sessionStorage.getItem to reference 'isImage1' 您需要将sessionStorage.getItem更改为引用“ isImage1”

var isImage1 = sessionStorage.getItem('isImage1');

Then you should get the value you are expecting. 然后,您应该获得期望的价值。

There are loads of good jsfiddles on session storage. 会话存储上有很多不错的jsfiddles。 you may get some ideas from this one: 您可能会从中得到一些想法:

http://jsfiddle.net/gabrieleromanato/XLRAH/ http://jsfiddle.net/gabrieleromanato/XLRAH/

Incidently; 顺便说一句, this is a very small value you are storing, why not store it in a cookie instead? 这是您要存储的很小的值,为什么不将其存储在cookie中呢?

EDIT: 编辑:

based on the fact that you have multiple functions exactly like this one, you are better off following Ken's solution, the only thing I would add is a wildcard to turn off the other images: 基于您具有与该功能完全相同的多种功能这一事实,您最好遵循Ken的解决方案,我唯一要添加的是通配符来关闭其他图像:

function showImage(imgNum) {
            sessionStorage.setItem('Image',imgNum);
            $("[id^=loadingImage]").hide();
            $("#loadingImage" + imgNum).show();

}

showImage(sessionStorage.getItem('Image'));

The code in the buttons would then be showImage(1) instead of showImage1(); 然后,按钮中的代码将是showImage(1)而不是showImage1();。

_Pez _Pez

By re-factoring the code a little you can do something like this: 通过稍微重构代码,您可以执行以下操作:

/// setup some vars including max number of images
var maxImages = 6, i = 1, v;

/// now loop through and get the items for each image
for(; i =< maxImages; i++) {
    v = sessionStorage.getItem('isImage' + i);

    /// if in storage, call show image with the number to show
    if (v !== null) showImage(i);
}

/// show image based on number
function showImage(num) {

    sessionStorage.setItem('isImage' + num, '1');
    $("#loadingImage" + num).show();
}

Also note that sessionStorage only deals with strings. 另请注意, sessionStorage仅处理字符串。 So in order to check a specific number you need to convert it to one first parseInt(value, 10); 因此,为了检查特定数字,您需要将其转换为一个第一个parseInt(value, 10); .

But in this case the 1 that we set can be anything - it's just to store some value so sessionStorage doesn't return null . 但是在这种情况下,我们设置的1可以是任何值-只是存储一些值,因此sessionStorage不会返回null

In the button code you can change it to do this: 在按钮代码中,您可以更改它以执行以下操作:

<input name="EPL/MECH DESIGN - TECHS" style="white-space:normal"  
onclick="moveText(this.name);showImage(1);form1.submit()" 
style="width: 275px"  type="button" value="7SBD EPL/Mech. Design Techs" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM