簡體   English   中英

jQuery處理來自變量的JSON字符串而沒有進行AJAX調用

[英]jQuery process JSON string from variable withou making AJAX call

我正在嘗試使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];
        }
    })
})

因為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];
    }
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM