使用Jquery将数据放入JSON [英] Put data into JSON with Jquery
本文介绍了使用Jquery将数据放入JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在从表单中提交的数据中构建一个JSON数组,但是此脚本正在中断,无法弄清楚是什么原因造成的!
I'm building a JSON array out of submitted data in a form, but this script is breaking and can't figure out what is causing it!
有什么想法吗?
jQuery:
var canvas = [];
var Submission = {};
var Answer = {};
$('.question-holder > input').each(function(){
answer = $(this).val()
input_name = $(this).attr('name')
label = $(this).closest('li').find('label')
// Push everything into the questions array
Answer.push({
Answers: answer,
Input_Name: input_name,
Label: label
});
});
$('.submit-holder > input').each(function(){
answer = $(this).val()
input_name = $(this).attr('name')
label = $(this).closest('li').find('label')
Submission.push({
Question: label,
Input_Name: input_name,
Submitted_data: answer
});
});
canvas.push({
Submission: Submission,
Answers: Answer,
Motivation: $('.motivation').val()
});
json = JSON.stringify({json: canvas}, null, "\t");
推荐答案
与声明有关的问题:
var Submission = {};
var Answer = {};
它们应该是这样的数组,对象(哈希)没有定义push()
They should be arrays like this, objects (hash) don't have push()
defined
var Submission = [];
var Answer = [];
这篇关于使用Jquery将数据放入JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文