如何使用jquery或javascript在表行中查找值?

时间:2018-01-19 04:08:18

标签: javascript jquery

我想在表格行中找到值。示例,我有一个这种情况

    $("tr[id*='row']").each(function (i, el) {
            //if($this.value() >10)
            //console.log('This value in cell is: ' + this.value);
        });
<div id='div1'>
    <table border='2px'>
        <tr>
            <td>1</td>
            <td>2</td>
        </tr>
        <tr>
            <td>3</td>
            <td>4</td>
        </tr>
    </table>
    <table border='2px'>
        <tr>
            <td>5</td>
            <td>6</td>
        </tr>
        <tr id='row' style="background-color:yellow; color:red">
            <td>7</td>
            <td>8</td>
        </tr>
    </table>
    <table border='2px'>
        <tr id='row' style="background-color:yellow; color:red">
            <td>9</td>
            <td>10</td>
        </tr>
        <tr>
            <td>11</td>
            <td>12</td>
        </tr>
    </table>
    <table border='2px'>
        <tr>
            <td>13</td>
            <td>14</td>
        </tr>
        <tr id='row' style="background-color:yellow; color:red">
            <td>15</td>
            <td>16</td>
        </tr>
    </table>
</div>

在上面这种情况下,我想比较并显示单元格中的值,超过10,但我在定义单元格中的值时感到困惑。 Mabe,你可以给我这个案子的建议。

2 个答案:

答案 0 :(得分:2)

find用于td值。

$("tr[id*='row']").find('td').each(function(i, el) {
var val = $(this).text();
if (val > 10)
  console.log('This value in cell is: ' + $(this).text());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='div1'>
    <table border='2px'>
        <tr>
            <td>1</td>
            <td>2</td>
        </tr>
        <tr>
            <td>3</td>
            <td>4</td>
        </tr>
    </table>
    <table border='2px'>
        <tr>
            <td>5</td>
            <td>6</td>
        </tr>
        <tr id='row' style="background-color:yellow; color:red">
            <td>7</td>
            <td>8</td>
        </tr>
    </table>
    <table border='2px'>
        <tr id='row' style="background-color:yellow; color:red">
            <td>9</td>
            <td>10</td>
        </tr>
        <tr>
            <td>11</td>
            <td>12</td>
        </tr>
    </table>
    <table border='2px'>
        <tr>
            <td>13</td>
            <td>14</td>
        </tr>
        <tr id='row' style="background-color:yellow; color:red">
            <td>15</td>
            <td>16</td>
        </tr>
    </table>
</div>

答案 1 :(得分:1)

&#13;
&#13;
$(document).ready(function(){
    $("tr[id*='row']").each(function () {
		var td = $(this).find("td");
		var countdeeptd = td.length;
		td.each(function(i,el){
			if(td.eq(i).text() > 10)
				console.log('This value in cell is: ', td.eq(i).text());
		});
	});
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='div1'>
    <table border='2px'>
        <tr>
            <td>1</td>
            <td>2</td>
        </tr>
        <tr>
            <td>3</td>
            <td>4</td>
        </tr>
    </table>
    <table border='2px'>
        <tr>
            <td>5</td>
            <td>6</td>
        </tr>
        <tr id='row' style="background-color:yellow; color:red">
            <td>7</td>
            <td>8</td>
        </tr>
    </table>
    <table border='2px'>
        <tr id='row' style="background-color:yellow; color:red">
            <td>9</td>
            <td>10</td>
        </tr>
        <tr>
            <td>11</td>
            <td>12</td>
        </tr>
    </table>
    <table border='2px'>
        <tr>
            <td>13</td>
            <td>14</td>
        </tr>
        <tr id='row' style="background-color:yellow; color:red">
            <td>15</td>
            <td>16</td>
        </tr>
    </table>
</div>
&#13;
&#13;
&#13;