javascript - How to avoid reading previous JSON array value which are stored in page before page refresh -
i want avoid reading previous objects pushed in json array. shown in image. i'm self learning these concepts. need help, right method add , read values.
also dont know how ask question technically. appreciate if tell me how question should asked. can atleast improve better understanding.
jquery
$("#click").click(function(event) { event.preventdefault(); var $form = $('#myform'); var $boxes =$("input[id=mycheckboxes]:checked").length; if($boxes==0) { alert("choose atleast 1 category"); } else if($form.valid() && $boxes>0) { //if form valid action performed var data = $( "#myform" ).serializearray();//serialize data var valuesarray = $('input:checkbox:checked').map( function() { return this.value; }).get().join(","); data.push({ name: 'panel', value: valuesarray}); //convert json array object var loginformobject = {}; $.each(data, function(i, v) { loginformobject[v.name] = v.value; }); array.push(loginformobject); alert("added successfully"); viewfunction(array); return false; } }) //view function function viewfunction(array) { console.log(array); var panel_arr = ["", "regular", "reduced fee", "limited assurance","court programs"]; var ul_block = $("<ul/>"); $.each(array, function(i, data) { var panels = data.panel.split(","); var uli_block = $("<ul/>"); $.each(panels, function(j, jdata) { var ulii_block = $("<ul/>"); $edit = $('<a/>').attr('href', 'javascript:;').addclass('btn btn-default active').attr('role', 'button').text('edit') .css('margin-left', 5); $del = $('<a/>').addclass('btn btn-default active').attr('role', 'button').text('delete') .css('margin-left', 5); $(ulii_block).append($("<li/>").html(data.ptitle).append($edit,$del)); $(uli_block).append($("<li/>").html('<span class="collapsable">'+panel_arr[panels[j]]+'</span>')).append(ulii_block); $edit.click(editfunction.bind(null, data));//bind data function }); $(ul_block).append($("<li/>").html('<span class="collapsable">'+data.gpanel+'</span>').append(uli_block)); }); $("#addmember").append(ul_block); $(".collapsable").click(function () { $(this).parent().children().toggle(); $(this).toggle(); }); $(".collapsable").each(function(){ $(this).parent().children().toggle(); $(this).toggle(); }); }
i made method compare between 2 of json objects:
//tempobj old object , newobj new json, function returns bool function isdifferentobj(tempobj, newobj) { var tempobjlength = object.keys(tempobj).length; var newobjlength = object.keys(newobj).length; if (newobjlength >= tempobjlength) { (var key in newobj) { if (typeof tempobj[key] != "undefined") { if (newobj[key] != tempobj[key]) { return true; } } else { return true; } } return false; } else { (var key in tempobj) { if (typeof newobj[key] != "undefined") { if (tempobj[key] != newobj[key]) { return true; } } else { return true; } } return false; } }
Comments
Post a Comment