2010-05-21 31 views
2

我需要tp序列化一組輸入元素,但我不能爲我的生活找出這個簡單的任務。jQuery JSON編碼輸入值集

我可以通過有針對性的投入使用迭代成功:

$("#tr_Features :input").each(function() { 
    ... 
} 

這裏是我的代碼,不工作:

var features = ""; 
$("#tr_Features :input").each(function() { 
    features += {$(this).attr("name"): $(this).val()}; 
} 

序列化整個窗體不會給我我需要什麼。表單比這個輸入子集多得多。這似乎應該是一個非常簡單的任務,但顯然編程晚到星期五下午是不是一件好事。

如果它是有幫助的,這裏的表單輸入我的定位目標:

<table cellspacing="0" border="0" id="TblGrid_list" class="EditTable" cellpading="0"> 
<tbody><tr class="FormData" rowpos="1"> 
    <td class="CaptionTD ui-widget-content">Cable Family</td> 
    <td class="DataTD ui-widget-content" style="white-space: pre;">&nbsp;<input type="text" value="" id="feature_id:8" name="feature_id:8"></td> 
</tr> 
<tr class="FormData" rowpos="1"> 
    <td class="CaptionTD ui-widget-content">Material</td> 
    <td class="DataTD ui-widget-content" style="white-space: pre;">&nbsp;<input type="text" value="" id="feature_id:9" name="feature_id:9"></td> 
</tr> 
<tr class="FormData" rowpos="1"> 
    <td class="CaptionTD ui-widget-content">Thread Size</td> 
    <td class="DataTD ui-widget-content" style="white-space: pre;">&nbsp;<input type="text" value="" id="feature_id:10" name="feature_id:10"></td> 
</tr> 
<tr class="FormData" rowpos="1"> 
    <td class="CaptionTD ui-widget-content">Attachment Style</td> 
    <td class="DataTD ui-widget-content" style="white-space: pre;">&nbsp;<input type="text" value="" id="feature_id:11" name="feature_id:11"></td> 
</tr> 
<tr class="FormData" rowpos="1"> 
    <td class="CaptionTD ui-widget-content">Feature</td> 
    <td class="DataTD ui-widget-content" style="white-space: pre;">&nbsp;<input type="text" value="" id="feature_id:12" name="feature_id:12"></td> 
</tr> 
<tr class="FormData" rowpos="1"> 
    <td class="CaptionTD ui-widget-content">Comments</td> 
    <td class="DataTD ui-widget-content" style="white-space: pre;">&nbsp;<input type="text" value="" id="feature_id:13" name="feature_id:13"></td> 
</tr> 

回答

6

顯然我所創造的只是一個對象,而且很長很難。這聽起來比它應該有的更糟糕,但這是事實。我需要做的只是在index/key處使用name屬性,並使用val()作爲值。

var data = new Object(); 
$("#tr_Features :input").each(function() { 
    data[$(this).attr("name")] = $(this).val(); 
}); 
return data; 

這工作。誰知道?!真的很簡單。你們都向我伸出一隻手來幫助我,並在星期五打電話升級。乾杯!

+0

我剛剛更新了我的答案,但我看到你明白了。如果你想要一個實際的JSON對象,你仍然需要將它串聯起來。你可以看到我的答案(超級簡單)。 – user113716 2010-05-21 22:24:19

+0

只是注意到你正在使用一個數組而不是一個對象。是否有這個原因。我認爲一般不建議使用數組作爲散列。 http://ajaxian.com/archives/javascript-associative-arrays-considered-harmful – user113716 2010-05-21 22:28:01

+0

我想有兩個原因。一個工作,兩個散列是我知道創建動態對象屬性的唯一方法。如果你知道更好的方法,我願意接受建議。您提供的鏈接似乎比原型更關注原型。 – gurun8 2010-05-21 23:23:05

2

你想+=數組...嘗試使用features.push或從$.each功能使用索引:features[i] = someval

編輯

我注意到,您的所有tr s(以及一些td s!)都有相同的ID。這也可能導致一些問題。確保你所有的ID都是獨一無二的。

+0

重複ID的好處。這是一個疏忽,並得到糾正。上面的代碼也是一個錯字。這個星期五的人感覺像星期一。謝謝。 – gurun8 2010-05-21 21:41:51

+1

聽起來像某人有一個星期五的情況:( – Gabriel 2010-05-21 22:04:20

+0

鑑於新的編輯這個答案不再真的適用更多。 – R0MANARMY 2010-05-21 22:28:36

2

+=不會將對象添加到JavaScript中的數組。你想要的是.push()

var features = new Array(); 
$("#tr_Features :input").each(function(){ 
    features.push({$(this).attr("name"): $(this).val()}); 
}); 

但是等等,還有更多!據我所知,jQuery不支持將對象轉換爲JSON字符串(雖然它支持解析JSON)。您需要爲此使用單獨的JSON庫,例如this one

+0

什麼'features.join(',');'做? – Jason 2010-05-21 21:12:16

+0

它會產生'「[object對象],[對象對象],[對象對象],[對象對象],...「' – 2010-05-21 21:28:52

+0

是的,想通了,只是檢查 – Jason 2010-05-21 21:44:03

8

編輯:

如果我正確理解你的問題,這會給你你想要的JSON對象。

請注意,它需要json2庫。

var features = {}; // Create empty javascript object 

$("#tr_Features :input").each(function() {   // Iterate over inputs 
    features[$(this).attr('name')] = $(this).val(); // Add each to features object 
}); 

var json = JSON.stringify(features); // Stringify to create json object (requires json2 library) 

感謝R0MANARMY指出問題中預期的json格式。


有沒有原因你不使用jQuery的.serializeArray()函數?您可以在元素的子集上運行它,而不是整個表單。

$("#tr_Features :input").serializeArray(); 

從文檔:http://api.jquery.com/serializeArray/

的.serializeArray()方法創建對象的JavaScript陣列,準備被編碼爲JSON字符串。

The。serializeArray()方法可以作用於選擇了單個表單元素的jQuery對象,如<input>,<textarea><select>

+1

這很漂亮,它不適用於這個問題,因爲'serializeArray'返回對象格式爲'{名稱:,值:},問題要求以「{:}」的形式出現。「 – R0MANARMY 2010-05-21 21:57:05

+0

@ R0MANARMY - 是的,我明白你的意思了。謝謝你的糾正。 – user113716 2010-05-21 22:09:19