在获得了一些很棒的帮助之后,我使用以下代码段构建了一个 JS 对象:
var noticeMap = $('#preExamNoticesTable tbody tr').map(function() {
var $cells = $(this).children();
return {
sequence: $cells.eq(0).children('input').val(),
noticeUID: $cells.eq(1).text()
};
});
生成的 noticeMap 示例如下所示(来自 firebug):
jQuery(Object { sequence="1", noticeUID="JP-L23013663997630352308"},
Object { sequence="3", noticeUID="JP-L22913664089460612172"},
Object { sequence="4", noticeUID="JP-L22913664090188631530"},
Object { sequence="2", noticeUID="JP-L22913664089408651799"})
当我尝试JSON.stringify(noticeMap)
它时,它的效果很好,除了它会向字符串中添加我不想通过我的 POST 传递的额外数据。它为每个值(来自 noticeMap 的对象)生成数字键,但在最后一个所需的 k:v 对之后,我的字符串还具有上下文 obj、length和prevObject obj。stringify()
这是我和POST之后的JSON :
{ "0": { "sequence": "1", "noticeUID": "JP-L23013663997630352308" }, "1": { "sequence": "3", "noticeUID": "JP-L22913664089460612172" }, "2": { "sequence": "4", "noticeUID": "JP-L22913664090188631530" }, "3": { "sequence": "2", "noticeUID": "JP-L22913664089408651799" }, "length": 4, "prevObject": { "0": {}, "1": {}, "2": {}, "3": {}, "length": 4, "prevObject": { "0": { "location": {}, "jQuery19105836315711643562": 1 }, "context": { "location": {}, "jQuery19105836315711643562": 1 }, "length": 1 }, "context": { "location": {}, "jQuery19105836315711643562": 1 }, "selector": "#preExamNoticesTable tbody tr" }, "context": { "location": {}, "jQuery19105836315711643562": 1 } }
我不知道是什么导致了这种情况发生。我开始假设它与 stringify 如何处理从对象的 obj 创建 JSON 字符串有关,但是我不能使用我noticeMaps()
现在工作得很好的方法。有任何想法吗?
谢谢!