2014-12-02 2 views
-1

데이터를 편집/변경할 수있는 양식이 있습니다. 데이터가 JSON 파일에서오고 다음과 같습니다json에서 html 양식 입력을 생성하는 방법은 무엇입니까?

data:{ 
    "contest_id": 26, 
    "question": { 
     "title": "what is the capital of germany?", 
     "question_id": 27, 
     "answers": [ 
      { 
      "answer_id": 76, 
      "answer": "Hamburg", 
      "correct" : "false"    
      }, 
      { 
      "answer_id": 77, 
      "answer": "Munich", 
      "correct" : "false" 
      }, 
      { 
      "answer_id": 78, 
      "answer": "Berlin", 
      "correct" : "true" 
      } 
     ] 
    } 
} 

내 기본 HTML은 (편집 팝업에서) 다음과 같습니다 : 그래서

// this is just a snippet of the entire form 

<input type="radio" id="answerChk_" name="correct"> 
<input type="text" required></input> 
<input type="hidden" id="answer_" value=""></input> 

, 내가 무엇을 달성하고자하는 가져하는 것입니다 대답 + answer_id를 삽입하고 대답이 맞는지 아닌지 -> 그러면 데이터베이스를 게시하고 업데이트 할 수 있습니다.

어떻게 가능합니까? 어떤 플러그인도 사용하고 싶지 않습니다 ...

아무쪼록 고맙습니다!

+0

어떤 검색어를 사용하셨습니까? – Jonast92

+0

@vch 당신은 절대적으로 옳습니다, 저의 실수는 ... 거기에 있어야만하는 것이 아닙니다 ... – Steve

+0

''님은 스스로 닫습니다 ... – War10ck

답변

1

당신은 루프 trought에게 배열을 할 수있는이 같은 HTML 추가 :

$.each(data.question.answers, function() { 
    $("YourForm").append('<input type="radio" id="answerChk_'+ this.answer_id +'" name="correct" selected="'+this.correct +'">'); 
    $("YourForm").append('<input type="text" required>'+ this.answer+'</input>'); 
    $("YourForm").append('<input type="hidden" id="answer_'+ this.answer_id +'" value="'+ this.answer+'"></input>'); 
}); 

난 당신이 아이디어를 얻을 희망을.

+0

고마워요! 이것은 나를 위해 작동합니다 :-) – Steve

관련 문제