0

次の問題があります。jquerymobileを使用してWebアプリを構築しています。webapp(index.html)のスタートアップページに、イベントのリストを表示しています。ユーザーがイベントをクリックすると、(eventsDetail.html)が読み込まれます。このページでは、ユーザーは「joinContestボタンをクリック」してコンテストに参加するオプションがあります。

質問: joinContestボタンを複数回クリックしてindex.htmlに戻り、.btn-showContestsボタンをクリックすると、1つのIDのみが保存されました(これは最後にクリックしたコンテストのIDです)。クリックしたコンテスト。index.htmlに戻って.btn-showContestsをクリックすると、クリックしたすべてのIDが取得されますか?

ファイル:eventdetails.js

$('.btn-joinContest').live('click', function(e){
    if (Modernizr.localstorage) {
        var id = getUrlVars()["id"];

        localStorage.setItem("contestId", id);
    } else {
        console.log('browser doesnt support localstorage')
    }   
});

ファイル:eventlist.js

注:ボタン.btn-showContestsをクリックすると、この関数が実行されます。

function showContests(){
    var contests = localStorage.getItem("contestId");
    if(contests == null) {
        console.log('Nothing in store');
    }
    else if (contests.length === 0) {
        console.log('Store contains empty value');
    }          
    console.log(contests.length);       
}
4

1 に答える 1

0

ボタンをクリックするたびに「contestId」を上書きしていると思います。クリックしたアイテムのリストを保持したい場合は、次のようにします。

var currentlist = localStorage.getItem("contestId");
currentlist = currentlist +" "+ id;
localStorage.setItem("contestId", currentlist);

またはより良いスタイルで..それを配列に格納します。

これがお役に立てば幸いです〜

于 2012-07-12T18:32:23.437 に答える