尝试在全局函数中设置动态JS变量

时间:2016-11-10 11:31:01

标签: javascript jquery variables scope global

我正在尝试使用动态变量设置本地存储值,这些动态变量将循环通过。基本上我只是想做这个(有效但不动态):

 localStorage.lvlZeroValue = localStorage.lvlZeroMaxValue;

使用:

counterMarkers[numberID] = maxMarkers[numberID];

但它并未影响全球范围内的“localStorage.lvlZeroValue”

$('#spellCountTackMax').click(function() {

    var counterMarkers = [
        localStorage.lvlZeroValue,
        localStorage.lvlOneValue,
        localStorage.lvlTwoValue,
        localStorage.lvlThreeValue,
        localStorage.lvlFourValue,
        localStorage.lvlFiveValue,
        localStorage.lvlSixValue,
        localStorage.lvlSevenValue,
        localStorage.lvlEightValue,
        localStorage.lvlNineValue
    ];

    var maxMarkers = [
        localStorage.lvlZeroMaxValue,
        localStorage.lvlOneMaxValue,
        localStorage.lvlTwoMaxValue,
        localStorage.lvlThreeMaxValue,
        localStorage.lvlFourMaxValue,
        localStorage.lvlFiveMaxValue,
        localStorage.lvlSixMaxValue,
        localStorage.lvlSevenMaxValue,
        localStorage.lvlEightMaxValue,
        localStorage.lvlNineMaxValue
    ];

        jQuery.fn.onTackSet = function(numberID){

            return this.each(function(){

                if(maxMarkers[numberID] == "" || maxMarkers[numberID] == null || maxMarkers[numberID] == 0 ) {

                    alert("not else ran");
                    $(this).attr('value', "0");
                    $('#spin' + numberID).attr('value', "0");
                    counterMarkers[numberID] = "0";
                    maxMarkers[numberID] = "0";



                } else {
                    alert("else ran");
                    $(this).attr('value', maxMarkers[numberID]);
                    $(this).attr('max', maxMarkers[numberID]);
                    // localStorage.lvlZeroValue = localStorage.lvlZeroMaxValue;
                    alert(counterMarkers[numberID]);
                    alert(maxMarkers[numberID]);

                    // this works but isn't dynamic
                    localStorage.lvlZeroValue = localStorage.lvlZeroMaxValue;

                    // my attempt at making it dynamic doesn't seem to work globally
                    counterMarkers[numberID] = maxMarkers[numberID];

                }
            });
        };

        $("#spin0").onTackSet(0);

所以我很确定我的问题是范围,但我似乎无法做到正确。请帮忙。谢谢!

2 个答案:

答案 0 :(得分:0)

您是否需要按照存储方式保存存储值的密钥?如果您可以将其从lvlZeroValue更改为lvl0Value,则可以使用以下方法:

    jQuery.fn.onTackSet = function(numberID){

            return this.each(function(){

                if(localStorage['lvl'+numberID+'MaxValue'] == "" || localStorage['lvl'+numberID+'MaxValue'] == null || localStorage['lvl'+numberID+'MaxValue'] == 0 ) {

                    alert("not else ran");
                    $(this).attr('value', "0");
                    $('#spin' + numberID).attr('value', "0");
                    localStorage['lvl'+numberID+'Value'] = "0";
                    localStorage['lvl'+numberID+'MaxValue'] = "0";



                } else {
                    alert("else ran");
                    $(this).attr('value', localStorage['lvl'+numberID+'MaxValue']);
                    $(this).attr('max', localStorage['lvl'+numberID+'MaxValue']);

                    localStorage['lvl'+numberID+'Value'] = localStorage['lvl'+numberID+'MaxValue'];

                }
            });
        };

如果您需要保留现有的密钥,您仍然可以通过构建不同的阵列来适应上述方法:

var markers = [
        'lvlZeroValue',
        'lvlOneValue',
        'lvlTwoValue',
        'lvlThreeValue',
        'lvlFourValue',
        'lvlFiveValue',
        'lvlSixValue',
        'lvlSevenValue',
        'lvlEightValue',
        'lvlNineValue'
    ];
var maxMarkers = [
        'lvlZeroMaxValue',
        'lvlOneMaxValue',
        'lvlTwoMaxValue',
        'lvlThreeMaxValue',
        'lvlFourMaxValue',
        'lvlFiveMaxValue',
        'lvlSixMaxValue',
        'lvlSevenMaxValue',
        'lvlEightMaxValue',
        'lvlNineMaxValue'
    ];

并像这样使用它们:

localStorage[markers[numberID]] = localStorage[maxMarkers[numberID]];

Here is a fiddle了解它是如何运作的。

答案 1 :(得分:0)

我想我已经解决了这个问题:

jQuery.fn.onTackSet = function(countLocation, numberID){

            return this.each(function(){

                if(maxMarkers[numberID] == "" || maxMarkers[numberID] == null || maxMarkers[numberID] == 0 ) {

                    // alert("not else ran");
                    $(this).attr('value', "0");

                    $('#spin' + numberID).attr('value', "0");
                    counterMarkers[numberID] = "0";
                    maxMarkers[numberID] = "0";



                } else {
                    // alert("else ran");
                    $(this).attr('value', maxMarkers[numberID]);
                    $(this).attr('max', maxMarkers[numberID]);
                    // localStorage.lvlZeroValue = localStorage.lvlZeroMaxValue;
                    alert(countLocation);
                    alert(maxMarkers[numberID]);

                    // this works but isn't dynamic
                    // localStorage.lvlZeroValue = localStorage.lvlZeroMaxValue;
                    localStorage.setItem(countLocation, maxMarkers[numberID]);
                    // my attempt at making it dynamic doesn't seem to work globally
                    // counterMarkers[numberID] = maxMarkers[numberID];

                }
            });
        };

        $("#spin0").onTackSet("lvlZeroValue", 0);

...但你的答案更清晰。非常感谢您的投入。我会按你的方式尝试。实际上我仍然需要更新if null,如果""这部分...