我正在从每个输入字段的值中创建一个JSON数组,但是数组中没有保存任何元素,并且警报(json)不会在底部触发。我做错了什么?
这是一个(不工作的小提琴)http://jsfiddle.net/PRnPd/
我在加载页面时遇到此控制台错误,位于JSON.stringify的行:“Uncaught InvalidStateError:尝试使用不可用或不再可用的对象。”
HTML
<div class="question-holder">
<div>
<label>First Question</label>
<input type="text" value="First" />
</div>
<div>
<label>Second Question</label>
<input type="text" value="Second" />
</div>
</div>
<div class="submit-holder">
<div>
<label>Name</label>
<input type="text" name="txtName" value="John Doe" />
</div>
<div>
<label>E-mail</label>
<input type="email" name="txtEmail" value="john@doe.com" />
</div>
<div>
<label>Second Question</label>
<input type="text" value="Second" />
</div>
</div>
<button class="get_json">Get JSON</button>
Jquery的:
$(document).on('click', '.get_json', function () {
var json = [];
var Submission = [];
var Answer = [];
$('.question-holder').find('input').each(function(){
answer = $(this).val()
label = $(this).closest('li').find('label')
Answer.push({
Answers: answer,
Label: label
});
});
$('.submit-holder').find('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
});
});
json.push({
Submission: Submission,
Answers: Answer,
Motivation: $('.motivation').val()
});
json = JSON.stringify({json: json}, null, "\t");
alert(json)
});