[英]Build JavaScript associative array and pass to PHP using each loop
been trying to to create an object in javascript using input values and id's It works but when i try to serialize / stringify the object becomes empty. 一直试图使用输入值和id在javascript中创建对象。它可以正常工作,但是当我尝试序列化/字符串化时,对象变为空。
My code: 我的代码:
dataArray = [];
$('.popover :input').each(function() {
dataArray[$(this).attr('id')] = $(this).val();
});
console.log(dataArray);
/*
RESULT:
Array[0]
home_recent_jobs:"123"
home_recent_jobs2:"345"
length:0
undefined:""
__proto__:Array[0]
*/
var json = $(dataArray).serialize();
var ser = JSON.stringify(dataArray);
console.log(json); // RESULT: EMPTY
console.log(ser); // RESULT: []
Ideas? 想法?
What am i doing wrong? 我究竟做错了什么?
I had this problem some time ago. 我前一段时间有这个问题。 Try to use this lib, it help me a lot
尝试使用此库,它对我有很大帮助
https://github.com/macek/jquery-serialize-object https://github.com/macek/jquery-serialize-object
Ok... after searching i found this solution - better each loop 好吧...搜索之后,我找到了这个解决方案-每个循环都更好
new each loop: 新的每个循环:
$('.popover :input').each(function (index) {
var cid = $(this).attr('id');
var cval = $(this).val();
if(cid && cval) {
var obj = {
id: cid,
val: cval
};
dataArray.push(obj);
}
});
This seems to do the trick. 这似乎可以解决问题。 Thanks for trying to help
感谢您的帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.