카테고리 없음
[자바 스크립트] 양식에서 json 만들기
행복을전해요
2021. 2. 18. 14:13
답변 목록 (질문 ID 포함)도 가지고 있는데 왜 답변 된 질문 목록을 원하는지 100 % 확실하지 않습니다. 그래도 요청한 것이므로 여기에 있습니다.
HTML
<h2>Form</h2>
<form action="" method="post">
Which city is in Great Britain?<br/>
<input type="hidden" name="first" value="1"/>
London:<input type="radio" name="first" data-questionid="1" value="11"/><br/>
New York:<input type="radio" name="first" data-questionid="1" value="12"/><br/>
Which city is in USA?<br/>
<input type="hidden" name="second" value="2"/>
Washington:<input type="radio" name="second" data-questionid="2" value="13"/><br/>
Tokio:<input type="radio" name="second" data-questionid="2" value="14"/><br/>
<p><input type="submit" /></p>
</form>
<h2>JSON</h2>
<pre id="result">
</pre>
자바 스크립트
$(function() {
$('form').submit(function() {
var voted_questions = [];
var answers = [];
$('input[type="radio"]:checked').each(function(){
voted_questions.push($(this).data('questionid'));
answers.push({'question':$(this).data('questionid'), 'options':this.value});
});
var data = {
'voted_questions': voted_questions,
'answers': answers
};
$('#result').text(JSON.stringify(data));
return false;
});
});
jsfiddle
출처
https://stackoverflow.com/questions/22079860