不渲染模板

时间:2016-06-13 07:37:02

标签: javascript render lodash

请帮助我在代码中找到语法错​​误,它不会使用loDash呈现。 单击按钮必须出现模态窗口,解释是选择的答案是否正确。

这是html:

 <script type="text/template" id="test">
  <div class="wrapper">
  <header><%= header %></header>
  <form action="#" method="POST">
    <ul>
   <% for (var i=0;i<questions.length;i++) {  %>
     <li><h3><%= questions[i].title %></h3>
    <% for (var j=0; j<questions[i].answers.length;j++){ %>
   <p>
    <input type="checkbox" name="<%= questions[i].checkName %>" id="<%= questions[i].id[j]%>"/>
    <label for="<%= questions[i].id[j] %>"<%= questions[i].answers[j]%></label>
    </p>
    <% }; %>
  </li>
  <% };%>
</ul>
   <button>Проверить мои результаты</button>

  </form>
  </div>

  </script>

这是script.js

"use strict";

var obj = {

header: "Some test",

questions:[
     {title:"Question №1",
      chekboxName:["one","two","three"],
      id:["1","2","3"],
      answers:["Case №1","Case №2","Case №3"],
      correct:1
      },
     {title:"Question №2",
      chekboxName:["one","two","three"],
     id:["1","2","3"],
      answers:["Case №1","Case №2","Case №3"],
      correct:2
      },
      {title:"Question №3",
      chekboxName:["one","two","three"],
      id:["1","2","3"],
      answers:["Case №1","Case №2","Case №3"],
      correct:3
      }
      ]
      };

var sObj = JSON.stringify(obj);
localStorage.setItem("object", sObj);

var retObj = JSON.parse(localStorage.getItem("object"));
var html = $('#test').html();
var content = tmpl(html,retObj);
$("body").append(content);

function showModal(e){
     e.preventDefault();
     var modal = $("div class='modal'></div>");
     var result = 0;
     var answer = $('input:checked');
     var correct = [];
     for (var i=0; i<retObj.questions.length;i++){
          correct[i] = retObj.questions[i].correct;
          if($(answer[i]).attr('id')==correct[i]){
               result+=1;
               modal.append('<p class="correct">Answer for '+ (i+1)+'question is correct</p>');
     } else {
          modal.append('<p class="incorrect">Answer for ' + (i+1) + 'question is incorrect</p>');
     }
}
modal.append('<button>Try again</button>');
$('body').append(modal);

$('button').one('click',function(e){
     e.preventDefault();
     modal.detach();
     $('input').attr('checked', false);

0 个答案:

没有答案