jquery append不附加来自for循环的大型html

时间:2013-02-21 13:13:57

标签: javascript jquery

我不清楚为什么当我要附加大量数据时,我没有获得任何数据。

当我有3个对象时工作正常,对于较大的数字,它不会附加数据。 我相信在for循环结束之前调用了append。可能是吗?

构建<tr>

for (var i = 0; i < obj.length; i++) {
    if (obj[i].indiceId === 4 && dinamicDom !== 'dinamic_index_busca_edit') {
        var dateChkBox = '<p class="pDtPeriodoChk"><input type="checkbox" name="dtPeriodoChk" class="dtPeriodoChk" id="' + i + '_chk">Por Período</p>';
        trs = trs + '<tr class="' + dinamicDom + '" style="padding-top:10px;"><td class="smallMediumField ' + i + '" style="padding-top:10px;"><fieldset class="dataFieldset"><legend>' + obj[i].indiceName + '</legend>' + dateChkBox + '<span id="' + i + '_lblDtInicio" class="lblDataInicio" style="display:none"><br />Data Inicio:</span><table><tr><td><img src="/Images/equal.png" alt="Igual a"/></td><td><input type="text" id="' + i + '" name="data" class="selectInput limpar" ';

        if (obj[i].valor !== null && obj[i].valor !== '') {
            trs = trs + ' value="' + obj[i].valor + '" ';
        }    
        trs = trs + '></td></tr></table><input type="hidden" id="' + obj[i].indiciId + '" name="data_indices" value="' + obj[i].indiciId + '" class="selectInput"></fieldset></td></tr>';
   } else {
       trs = trs + '<tr class="' + dinamicDom + '"><td class="smallMediumField ' + i + '" style="padding-top:10px;">' + obj[i].indiceName + '<br /><input type="text" id="' + i + '" name="groupBusca" class="selectInput limpar" ';
       if (obj[i].valor !== null && obj[i].valor !== '') {
           trs = trs + ' value="' + obj[i].valor + '" ';
       }
       trs = trs + '></td></tr></table><input type="hidden" id="' + obj[i].indiciId + '" name="indicesBusca" value="' + obj[i].indiciId + '" class="selectInput"></td></tr>';
   }

}

附加

$('#tblBusca').append(trs);

1 个答案:

答案 0 :(得分:0)

尝试逐行追加

for (var i = 0; i < obj.length; i++) {
    var trs;

    if (obj[i].indiceId === 4 && dinamicDom !== 'dinamic_index_busca_edit') {
        var dateChkBox = '<p class="pDtPeriodoChk"><input type="checkbox" name="dtPeriodoChk" class="dtPeriodoChk" id="' + i + '_chk">Por Período</p>';
        trs = trs + '<tr class="' + dinamicDom + '" style="padding-top:10px;"><td class="smallMediumField ' + i + '" style="padding-top:10px;"><fieldset class="dataFieldset"><legend>' + obj[i].indiceName + '</legend>' + dateChkBox + '<span id="' + i + '_lblDtInicio" class="lblDataInicio" style="display:none"><br />Data Inicio:</span><table><tr><td><img src="/Images/equal.png" alt="Igual a"/></td><td><input type="text" id="' + i + '" name="data" class="selectInput limpar" ';

        if (obj[i].valor !== null && obj[i].valor !== '') {
            trs = trs + ' value="' + obj[i].valor + '" ';
        }    
        trs = trs + '></td></tr></table><input type="hidden" id="' + obj[i].indiciId + '" name="data_indices" value="' + obj[i].indiciId + '" class="selectInput"></fieldset></td></tr>';
   } else {
       trs = trs + '<tr class="' + dinamicDom + '"><td class="smallMediumField ' + i + '" style="padding-top:10px;">' + obj[i].indiceName + '<br /><input type="text" id="' + i + '" name="groupBusca" class="selectInput limpar" ';
       if (obj[i].valor !== null && obj[i].valor !== '') {
           trs = trs + ' value="' + obj[i].valor + '" ';
       }
       trs = trs + '></td></tr></table><input type="hidden" id="' + obj[i].indiciId + '" name="indicesBusca" value="' + obj[i].indiciId + '" class="selectInput"></td></tr>';
   }

    $('#tblBusca').append(trs);
}