开发者

Javascript: How to Keep Persistant Array Data

I'm trying to build an array in javascript that contains a list of ids. Here is a simplified version of the function that I have...

function updateCost(id) {
    var inputArray = [];
    inputArray.push(id);
}

And here is the html code...

<input type="file" name="field1" id="field1" onchange="updateCost('1');" />
<input type="file" name="field2" id="field2" onchange="updateCost('2');" />
<input type="file" name="field3" id="field3" onchange="updateCost('3');" />
...

What I would like to happen, is each time I select a file from my computer using one of the file input fields that I have, I would like the id of that input field to be stored in the inputArray array. However, every time I select a file, it adds that id to the array, but it doesn't append it to the array, so everything else in the array is deleted, leaving only one entry in the array.

Is there any way to make the array data persistent so it will grow each time I use开发者_JAVA百科 one of the of the file input fields?

Thanks!


(function() {


   var inputArray = [];
   function updateCost(id) {
    inputArray.push(id);
   }


})();

Define it outside the scope of the function. You don't really need the enclosing anon function around, that's just my personal style.


function updateCost(id) {
    if(typeof updateCost.inputArray=="undefined")updateCost.inputArray=[]
    updateCost.inputArray.push(id);
}

Make Array is "Static"

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜