私の問題: 以下のコードは regId に 1 つの値を格納します。もう一度ボタンをクリックすると、この値が上書きされます。regId 配列の他の値を上書きせずに、新しい値を regId に保存するにはどうすればよいですか?
$('.btn-joinContest').live('click', function(e){
if (Modernizr.localstorage) {
var id = getUrlVars()["id"],
regId = [ ],
currentList = localStorage["contest"];
if(currentList == null) {
console.log('first click add to local storage');
regId.push(id);
localStorage['contest']=JSON.stringify(regId);
}
else if (currentList.length === 0) {
console.log('store contains empty value');
regId.push(id);
localStorage['contest']=JSON.stringify(regId);
} else {
console.log('there is something in localstorage get it');
regId = JSON.parse(currentList);
// Search for a specified value within regId array and return its index
// (or -1 if not found).
if(jQuery.inArray(id, regId) > -1){
console.log('id is already in array');
} else {
console.log('add current id to localstorage');
regId.push(id);
localStorage['contest']=JSON.stringify(regId);
}
}
} else {
console.log('this browser does not support localstorage')
}
});