jQuery return the second table cell after the match?
This code is working, but I think it's anchoring on the wrong tag. Here I believe it's saying on the first match of aaaaa, return the cell value for the next div, because it happens to be under a dive. But now that I also want to pull the value of 3rd td on a match. How can I change this code so that I can use it get both of the subsequent cells when a match is found.
var bodyprefix = $('#issuetbl td:contains(aaaa)').siblings().find('div').html();
For example, given aaaa, I want var1=whatever and var2=cccc
I'm okay with running it twice, but would like the same function based on index or nth-child.
<tr>
<td>aaaa</td>
<td><div>开发者_开发问答;whatever</div></td>
<td>cccc</td>
</tr>
<tr>
<td>1111</td>
<td><div>something</div></td>
<td>2222</td>
</tr>
Use nextAll
instead of siblings
:
var bodyprefixes = [];
$('#issuetbl td:contains(aaaa)').nextAll().find('div').each(function(i, k) {
bodyprefixes.push($(k).html());
});
bodyprefixes
is now an array of all the values.
This is a little more than what you asked for with portions for illustrative purposes, but allows for more flexibility:
<script type="text/javascript">
$(document).ready(function(){
var str = 'aaaa';
var bodyprefixes = [];
$("tr").each(function(k,v){
var tds = $(this).children();
var len = tds.length;
$.each(tds, function(x,y){
b = $(y).html();
if(b == str){
for(var i=0;i<len-1;i++){
bodyprefixes.push($(tds[i+1]).html());
}
}
});
});
$.each(bodyprefixes, function(u,w){
$("#variables").append("Var"+u+"= "+w+" <br>");
});
});
</script>
<table id="issuetbl" border="1">
<tr>
<td>aaaa</td> <td><div>whatever</div></td> <td>cccc</td> </tr>
<tr> <td>1111</td> <td><div>something</div></td> <td>2222</td> </tr>
</table>
<div id="variables"></div>
精彩评论