Jquery - JSON.stringify,数组为空 [英] Jquery - JSON.stringify, array is empty
问题描述
我希望有人可以帮助我,帖子中的数组值为空。
i hope somebody can help me, the array value is empty in the post.
$(function start() {
c_all = new Array('#div { font-color:#ff0000; border:1px solid #00ff00; }', '#div_2 { font-color:#ff0000; }', '.line2 { font-color:#00ffff; }');
css(c_all);
});
function css(x) {
values = new Array();
for (i = 0; i < x.length; i++) {
c0_selector = '' + x[i].match(/^.*{/) + '';
c0_selector = c0_selector.replace(/\s*/g, '');
c0_selector = c0_selector.replace(/{/, '');
x[i] = x[i].replace(/^.*{/, '');
x[i] = x[i].replace(/}/, '');
c0_arr = x[i].split(';');
values['' + c0_selector + ''] = new Array();
$('#log').append(''+c0_selector+'<br />');
for (i2 = 0; i2 < c0_arr.length; i2++)
{
values[''+c0_selector+''][i2] = c0_arr[i2].split(':');
$('#log').append(''+c0_arr[i2]+'<br />');
}
}
$.ajax({
type: 'post',
data: JSON.stringify(values),
contentType: 'application/json',
dataType: 'json'
});
}
工作示例 - > http://www.jsfiddle.net/V9Euk/448/
提前致谢!
Peter
Thanks in advance! Peter
推荐答案
尝试制作值
一个对象,(喜欢它应该在javascript中用于命名键。)
Try making values
an Object, (like it should be in javascript for named keys).
var values = {};
此外,使用以下方式声明变量是真的好主意 var
关键字,因此您不会创建全局变量。
Also, it is a really good idea to declare your variables with the var
keyword, so you're not creating global variables.
此外,不需要''+ c0_selector +''
因为你已经有了一个字符串。只需执行 c0_selector
。
Also, no need for '' + c0_selector + ''
since you already have a String. Just do c0_selector
.
成品记录填充的对象。 http://www.jsfiddle.net/V9Euk/450/
Finished product logs the populated Object. http://www.jsfiddle.net/V9Euk/450/
这篇关于Jquery - JSON.stringify,数组为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!