在js文件中绘制html

时间:2012-07-17 15:30:28

标签: html

我必须保持div和内部我必须得到答案然后div应该关闭。即使在循环中的问题之后关闭div之后,div div也会在没有答案的情况下关闭。 代码:

if(a.isQuestion == "true"){
                  ansdetails="";
                  ansdiv ="";
                  $.each(a.answers, function(key, b) {
                      if(a.anstype=="select only one"){
                          ansdetails +="<div id=\"" +b.nextuid+ "\" data-link=\"Answer" +a.index+ "\"  class=\"showfirst\"><tr><th width=\"20\" height=\"40\" align=\"left\" valign=\"top\" scope=\"row\" class=\"questionnaireanswer\"><input name=\"radiobuttton\" answered=\"\" current-index=\""+a.index+"\" value=\"" +b.ansuid +"\"  data-value=\"" +b.ansuid+ "\" class=\"\" type=\"radio\">" +b.ansname+ " .</th>\
                          <td width=\"438\" height=\"40\" align=\"left\" valign=\"top\" class=\"questionnaireanswer\"> " +b.ansvalue+ "</td></tr></div>";
                      }
                      else{
                          ansdetails +=" <div id=\"Answer" +a.index+ "\"  class=\"showfirst\">\<tr>\
                          <th width=\"20\" height=\"40\" align=\"left\" valign=\"top\" scope=\"row\" class=\"questionnaireanswer\"><input name=\"chk\" current-index=\""+a.index+"\" id=\"" +b.ansuid +"\" value=\"" +b.nextuid+ "\" data-value=\"" +b.ansuid+ "\" class=\"\" type=\"checkbox\">" +b.ansname+ " .</th>\
                          <td width=\"438\" height=\"40\" align=\"left\" valign=\"top\" class=\"questionnaireanswer\"> " +b.ansvalue+ "</td></tr>";
                      }
                  });            

              }

结果:

<div id=/"/"></div>
<tr><td><input type=/"radio/" name="dd" value=/"/">Answer 1</td></tr>
<tr><td><input type=/"radio/" name="dd" value=/"/">Answer 2</td></tr>
<tr><td><input type=/"radio/" name="dd" value=/"/">Answer 3</td></tr>
<tr><td><input type=/"radio/" name="dd" value=/"/">Answer 4</td></tr>

预期结果:

<div id=/"/">
<tr><td><input type=/"radio/" name="dd" value=/"/">Answer 1</td></tr>
<tr><td><input type=/"radio/" name="dd" value=/"/">Answer 2</td></tr>
<tr><td><input type=/"radio/" name="dd" value=/"/">Answer 3</td></tr>
<tr><td><input type=/"radio/" name="dd" value=/"/">Answer 4</td></tr>
</div>

2 个答案:

答案 0 :(得分:2)

<tr>元素不能是<div>元素的子元素(只有table / thead / tbody / tfoot)。尝试生成有效的HTML。

答案 1 :(得分:0)

如Quentin所述,您的表生成包含错误。有效语法更像是:

<div>
    <table>
        <tr>
            <td>
                <label><input type="radio" name="dd" value="">Answer 1</label>
            </td>
        </tr>
        <tr>
            <td>
                <label><input type="radio" name="dd" value="">Answer 2</label>
            </td>
        </tr>
        <tr>
            <td>
                <label><input type="radio" name="dd" value="">Answer 3</label>
            </td>
        </tr>
    </table>
</div>

无论如何,生成列表可能更容易,更易读:

<ul>
    <li>
        <label><input type="radio" name="dd" value="">Answer 1</label>
    </li>
    <li>
        <label><input type="radio" name="dd" value="">Answer 2</label>
    </li>
    <li>
        <label><input type="radio" name="dd" value="">Answer 3</label>
    </li>
</ul>