将新单击的值“type”保存到jsonObject文件中?

时间:2014-02-24 12:20:24

标签: javascript html jsonobject

我希望能够将新的点击值(类型)保存到我的jsonObject文件中 在菜单中,我可以选择类型值cre,在选择c时,我想将该值(类型)保存到我的jsonObject档案。

function populateTypes() {
    for (var k in jsonObjects) {
        var type = jsonObjects[k].type;
        if (availableTypes.indexOf(type) < 0) {
            availableTypes.push(type);
            $('#changetotypes').append('<li><a class="typechange" href="#" data-type="' + type + '">' + type + '</a></li>');
       }
    }
}

$(document).on('click', '.typechange', function (e) {
    e.preventDefault();
    var type = $(this).data('type');
    $('#currenttype').html('Aktuell Typ: ' + type);
});

这是我的jsonObject文件:

"c3000": {"x": 675, "y": 269, "plan":1, "name":"c3000", "img":"sensor3.png", "added":"datetime", "type":"c", "interval":"0", "comment":"enmassatext", "active":true, "value":"4c", "radie":false, "alarm":false},
"r3002": {"x": 223, "y": 355, "plan":1, "name":"r3002", "img":"sensor2.png", "added":"datetime", "type":"r", "interval":"6", "comment":"enmassatext", "active":true, "value":"-4c", "radie":false, "alarm":true},
"r3003": {"x": 300, "y": 100, "plan":1, "name":"r3003", "img":"sensor1.png", "added":"datetime", "type":"e", "interval":"3", "comment":"enmassatext", "active":false, "value":"15c", "radie":false, "alarm":false},

1 个答案:

答案 0 :(得分:0)

假设您网页上的每个元素都有一个id

$(document).on('click', '.typechange', function (e) {
    var el = $(this);

    jsonObjects[el.attr("id")] = {
      // populate your object here
      "data": el.data('type');
    };

    e.preventDefault();    
    var type = $(this).data('type');
    $('#currenttype').html('Aktuell Typ: ' + type);
});

这将更新您的JSON对象,现在您必须弄清楚如何将其写回原始JSON文件。