2010-05-13 92 views
5

我正在構建一個動態表單來編輯json對象中的數據。首先,如果存在這樣的事情讓我知道。我寧願不建立它,但我已經搜索了很多次的工具,並找到只需要輸入引號的樹狀結構。我會很樂意將所有的值視爲字符串。這種編輯功能適用於最終用戶,因此它需要簡單且不會令人生畏。將嵌套的json對象值綁定到表單字段

到目前爲止,我的代碼生成嵌套表來表示一個json對象。對於每個值我顯示一個表單域。我想將表單字段綁定到關聯的嵌套json值。如果我可以存儲對json值的引用,我會爲json對象樹中的每個值構建一個引用數組。我還沒有找到一種方法來做到這一點與JavaScript。

我最後的手段是在編輯完成後遍歷表格。我寧願動態更新,但單一提交會比沒有更好。

任何想法?

// the json in files nests only a few levels. Here is the format of a simple case, 
{ 
"researcherid_id":{ 
    "id_key":"researcherid_id", 
    "description":"Use to retrieve bibliometric data", 
    "url_template" :[ 
    { 
     "name": "Author Detail", 
     "url": "http://www.researcherid.com/rid/${key}" 
    } 
    ]   
} 
} 

$.get('file.json',make_json_form); 

function make_json_form(response) { 

    dataset = $.secureEvalJSON(response); 
    // iterate through the object and generate form field for string values. 

} 

// Then after the form is edited I want to display the raw updated json (then I want to save it but that is for another thread) 

// now I iterate through the form and construct the json object 
// I would rather have the dataset object var updated on focus out after each edit. 

function show_json(form_id){ 
var r = {}; 
var el = document.getElementById(form_id); 
table_to_json(r,el,null); 
$('body').html(formattedJSON(r)); 
} 
+2

所以你想要生成基於對象的表單並將表單字段綁定到對象屬性?對象結構是怎樣的? JSON只是一個字符串表示形式,您會發現只需處理Javascript對象並在需要時序列化爲JSON即可。 – Anurag 2010-05-13 21:42:39

回答

0

更簡單的方法是接受表單提交併以JSON格式輸出數據。這樣,就沒有必要綁定變量。