我需要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;"> <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;"> <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;"> <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;"> <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;"> <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;"> <input type="text" value="" id="feature_id:13" name="feature_id:13"></td>
</tr>
我剛剛更新了我的答案,但我看到你明白了。如果你想要一個實際的JSON對象,你仍然需要將它串聯起來。你可以看到我的答案(超級簡單)。 – user113716 2010-05-21 22:24:19
只是注意到你正在使用一個數組而不是一個對象。是否有這個原因。我認爲一般不建議使用數組作爲散列。 http://ajaxian.com/archives/javascript-associative-arrays-considered-harmful – user113716 2010-05-21 22:28:01
我想有兩個原因。一個工作,兩個散列是我知道創建動態對象屬性的唯一方法。如果你知道更好的方法,我願意接受建議。您提供的鏈接似乎比原型更關注原型。 – gurun8 2010-05-21 23:23:05