jQuery使用循环生成动态表

时间:2013-06-28 16:21:36

标签: javascript jquery

我在这里做错了什么:

var htmlString ="\
                <table width='97%' height='233px' border='1' class='NewTable'>\
                      <tr>\
                        <th width='43%'>"+currGroupAry[j]+"</th>\
                        <th width='8%'>Points</th>\
                        <th width='49%'>Justification</th>\
                      </tr>\
                      <tr>\
                        <td height='29'>Was willing to frequently share ideas and resources</td>\
                        <td><input id='TotalTEP1' type='text' value='' readonly /></td>\
                        <td>\
                            <table id='gp1' width='100%'>\
                            "for(var i=0; i<=currGroupAry.length; i++){"\
                                <tr><td style='width:100px;'>"+currGroupAry[j]+"</td><td><textarea style='width:97%; font-size: 14px; background-color: transparent; border:none; resize:none;' cols='10' rows='2' readonly>"+user.comments[j-1]+"</textarea></td></tr>"}"\
                            </table>\
                        </td>\
                      </tr>\</table>"

尝试将此表附加到Div中。但是不行。中间的For循环有问题。

1 个答案:

答案 0 :(得分:2)

这就是我要做的...给出你当前的代码....但是再一次....它不是我真的会怎么做.....

var htmlString ="<table width='97%' height='233px' border='1' class='NewTable'><tr><th width='43%'>"+currGroupAry[j]+"</th><th width='8%'>Points</th><th width='49%'>Justification</th></tr><tr><td height='29'>Was willing to frequently share ideas and resources</td><td><input id='TotalTEP1' type='text' value='' readonly /></td><td><table id='gp1' width='100%'>";

for(var i=0; i<=currGroupAry.length; i++){
htmlString+="<tr><td style='width:100px;'>"+currGroupAry[i]+"</td><td><textarea style='width:97%; font-size: 14px; background-color: transparent; border:none; resize:none;' cols='10' rows='2' readonly>"+user.comments[i-1]+"</textarea></td></tr>";
 }

htmlString+="</table></td></tr></table>";

另外....不确定您是否需要currGroupAry[j]currGroupAry[i],在循环中,您有j,但我更改为i,假设您需要循环显示相关信息,但如果那是你想要的,则改回j