2

jQueryイベントをDBに保存しようとしています。

//Calling this Function On Click 

function trackevent(event){
    window.events.push(event)
}

$.each(window.events, function(i, item){
    console.log(i +" - "+ $.parseJSON(item));
});

イベントは配列に保存されました。かっこいいですが、ループしようとするとwindow.events、jQueryイベントのJSONを取り戻すことができません。私は自分の間違いを理解することができません。どんな提案も役に立ちます。

ここに画像の説明を入力してください

parseJSONをJSON.stringifyに変更した場合、このエラーが発生します

$.each(window.events, function(i, item){
    console.log(i +" - "+ JSON.stringify(item));
});

ここに画像の説明を入力してください

ここに画像の説明を入力してください

$(document).ready(function(){
window.events = []
// Function to enable the hidden checkbox button
$(".answers_checkbox").click(function(){
    current_click = $(this).attr("data-attr");

    // If the Hidden checkbox is Checked , it means its already selected (blue)
    if($("."+this.id).attr('checked') == 'checked') {
        $(this).css('background-image', '');
    } else {
        // Changing the Background Image to current_click option
        $(this).css('background-image',"url('/assets/choice_buttons/choice"+current_click+"_Sq_Blue.png')");
    }

    $("."+this.id).attr('checked',!$("."+this.id).attr('checked'));
    trackevent(event);
});

function trackevent(event){
    window.events.push(event)
}

});

// Function to Save Events
$(window).bind('beforeunload', function(){
    $.each(window.events, function(i, item){
        console.log(i +" - "+ item);
    });
});
4

1 に答える 1

2

parseJSONJSONの文字列を解析し、オブジェクトを返します。あなたはそれにオブジェクトを渡すことになっていない。

とにかくコンソールにログを記録しているので、文字列表現の代わりに完全なオブジェクトをログに記録することもできます。

console.log(i, item);

これがデモンストレーションです:http://jsfiddle.net/HjcGT/1/

于 2013-01-01T16:52:20.253 に答える