我正在尝试让jQuery从变量处理JSON字符串而不进行AJAX调用,因为这个JSON字符串将从JSON2HTML生成,但jQuery.getJSON
需要进行AJAX调用,这是浪费资源。有没有人有这个问题的解决方案或解决方法?
从JSON2HTML生成的JSON字符串。
[{"tag":"div","id":"0","class":"sgph","children":[
{"tag":"div","data-scoretype":"","class":"sgi","id":"1","children":[
{"tag":"button","data-btnrole":"satis_group","class":"up","html":"up"},
{"tag":"button","data-btnrole":"satis_group","class":"down","html":"down"},
{"tag":"button","data-btnrole":"satis_group","class":"edit","html":"change"},
{"tag":"button","data-btnrole":"satis_group","class":"del","html":"delete"}
]},
{"tag":"div","class":"scph","children":[
{"tag":"button","data-btnrole":"score_type","class":"add","html":"score type"}
]},
{"tag":"div","class":"stph","children":[
{"tag":"div","class":"sti","id":"1","children":[
{"tag":"button","data-btnrole":"satis_topics","class":"up","html":"up"},
{"tag":"button","data-btnrole":"satis_topics","class":"down","html":"down"},
{"tag":"button","data-btnrole":"satis_topics","class":"edit","html":"change"},
{"tag":"button","data-btnrole":"satis_topics","class":"del","html":"delete"}
]},
{"tag":"div","class":"sti","id":"3","children":[
{"tag":"button","data-btnrole":"satis_topics","class":"up","html":"up"},
{"tag":"button","data-btnrole":"satis_topics","class":"down","html":"down"},
{"tag":"button","data-btnrole":"satis_topics","class":"edit","html":"change"},
{"tag":"button","data-btnrole":"satis_topics","class":"del","html":"delete"}
]},
{"tag":"button","data-btnrole":"satis_topics","class":"add","html":"add topic"}
]}
]},
{"tag":"div","id":"1","class":"sgph","children":[
{"tag":"button","data-btnrole":"satis_group","class":"add","html":"add group"}
]}
]
我做的jQuery代码。
//json_str will store JSON string which had been generate from JSON2HTML.
var json_str=$.parseJSON(FormatJSON(toTransform($('div.satis_form').children())));
$.getJSON(json_str, function(data){
$.each(data,function(i,val){
if(val.tag == 'button'){
delete val[i];
}
})
})
答案 0 :(得分:0)
代码应该是这样的,因为json_str
是JSON对象。
var json_str=$.parseJSON(FormatJSON(toTransform($('div.satis_form').children())));
$.each(data,function(i,val){
if(val.tag == 'button'){
delete val[i];
}
})