我想获取输入字段值,并最终将它们放入Javascript中(通过Javascript)。
这是我的html标记
<input type="text" class="specifications" name="specifications[0][title]" />
<input type="text" class="specifications" name="specifications[0][stat]" />
<input type="text" class="specifications" name="specifications[1][title]" />
<input type="text" class="specifications" name="specifications[1][stat]" />
<input type="text" class="specifications" name="specifications[2][title]" />
<input type="text" class="specifications" name="specifications[2][stat]" />
<input type="text" class="specifications" name="specifications[3][title]" />
<input type="text" class="specifications" name="specifications[3][stat]" />我希望我的JSON字符串是这样的
[
{ title: 'title1', stat: '1000' },
{ title: 'title1', stat: '2000' },
{ title: 'title2', stat: '3000' },
{ title: 'title3', stat: '4000' },
{ title: 'title4', stat: '5000' }
]我已经搜索了很多,找到了这些线程,但是它们并没有帮助我完成我想要完成的任务。
请帮帮忙。
发布于 2013-04-24 10:10:15
这将从您拥有的输入中创建您描述的对象:
var specsLen = $('input.specifications').length / 2,
array = [],
i;
for (i = 0; i < specsLen; i += 1) {
array.push({
title: $('input.specifications[name="specifications[' + i + '][title]"]').val(),
stat: $('input.specifications[name="specifications[' + i + '][stat]"]').val()
});
})见小提琴示例
https://stackoverflow.com/questions/16188800
复制相似问题