javascript - Cannot find elements that are dynamically created by Ajax -
in jsp page, use ajax call insert table body existing table. have jquery function find max value in rows in table , change background color. however, seems jquery cannot find elements dynamically created ajax.
ajax call:
function getreltable(type) { $.ajax({ type : "post", datatype : "html", cache : false, url : _ctx + "/monitor/get_table.do", data : { "type" : type, }, success : function(content) { //var t_body = $(content); $("#table_body").html(content); findmax(); }, error : function() { ui.msg('信息加载错误', 0); } }); }
and content in ajax jsp page:
<c:foreach var="noderel" items="${noderelinfo}" varstatus="stc"> <c:choose> <c:when test="${stc.first}"> <tr> <td style="vertical-align: middle; text-align: center;" rowspan="3">${stc.count}</td> <td style="vertical-align: middle; text-align: center;" valign="middle" align="center" rowspan="3">${noderel.fromnodename} --> ${noderel.tonodename}</td> <td></td> <c:foreach var="x" begin="0" end="23" step="1"> <td>${x}</td> </c:foreach> </tr> <tr class="value_row"> <td>${noderel.fromnodename}发送</td> <c:foreach var="node" items="${noderel.sendfinallist}"> <td class="value_cell" value="${node}"><c:choose> <c:when test="${empty node}"> - </c:when> <c:otherwise> ${node} </c:otherwise> </c:choose></td> </c:foreach> </tr> <tr class="value_row"> <td>${noderel.tonodename}发送</td> <c:foreach var="node" items="${noderel.recvfinallist}"> <td class="value_cell" value="${node}"><c:choose> <c:when test="${empty node}"> - </c:when> <c:otherwise> ${node} </c:otherwise> </c:choose></td> </c:foreach> </tr> </c:when> <c:otherwise> <tr class="value_row"> <td style="vertical-align: middle; text-align: center;" rowspan="2">${stc.count}</td> <td style="vertical-align: middle; text-align: center;" valign="middle" align="center" rowspan="2">${noderel.fromnodename} --> ${noderel.tonodename}</td> <td>${noderel.fromnodename}发送</td> <%-- <c:foreach var="node" items="${noderel.sendfinallist}"> <td class="value_cell" value="${node}"><c:choose> <c:when test="${empty node}"> - </c:when> <c:otherwise> ${node} </c:otherwise> </c:choose></td> </c:foreach> --%> <c:foreach var="x" begin="0" end="23" step="1"> <td value="${x}">${x}</td> </c:foreach> </tr> <tr class="value_row"> <td>${noderel.tonodename}发送</td> <c:foreach var="node" items="${noderel.recvfinallist}"> <td class="value_cell" value="${node}"><c:choose> <c:when test="${empty node}"> - </c:when> <c:otherwise> ${node} </c:otherwise> </c:choose></td> </c:foreach> </tr> </c:otherwise> </c:choose> </c:foreach>
however, in main page, following function doesn't work because dynamically created elements cannot found!
function findmax() { $('tr').each(function(){ //get first value var max = $(this).find('td.value_cell').first().attr("value"); if(isnan(parseint(max))) return; var highestelement = $(this).find('td.value_cell').first(); $(this).find('td.value_cell').each(function(){ var current = $(this).attr("value"); if(!isnan(parseint(current))){ if(current >= max) { max = current; highestelement = $(this); } } }); if(max != 0) highestelement.css('background-color', 'red'); }); }
what's wrong code?
update:
the answer here suggests can't select elements created in success call function. there other way find max value in dynamically created content , manipulate content?
Comments
Post a Comment