在动态表中获取某个TD的最高值

时间:2017-05-18 12:18:15

标签: javascript jquery html

我有一个像bellow这样的表结构:

function highestPosInBlock(posId) {
  // determine the highest number in the current block
  console.log($("body").data("block")); // ...
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table data-block="1">
  <tr>
    <td data-position="1">1</td>
  </tr>
  <tr>
    <td data-position="2">2</td>
  </tr>
  <tr>
    <td data-position="3">3</td>
  </tr>
</table>

<table data-block="2">
  <tr>
    <td data-position="1">1</td>
  </tr>
  <tr>
    <td data-position="2">2</td>
  </tr>
</table>

我想检索给定Block的最高data-position属性。到目前为止,我的JS函数看起来像这样:

function highestPosInBlock(posId) {
    // determine the highest number in the current block
    console.log($("body").data("block") // ...
}

这正是我被困住的地方。我传递的参数是给定的数据块。例如,我会调用这样的函数:

highestPosInBlock(1)我想要检索3

如果我打电话给highestPosInBlock(2),请回复2

知道怎么做吗?我完全停电了。

  

另一件事:我不能在jQuery中使用each-iterator,因为这个狗屎必须在IE 7中工作,它不支持每个()

1 个答案:

答案 0 :(得分:1)

Bellow例子我们得到了块1的最大位置,

这是通过获取表(通过其data-block属性)然后循环抛出它的td(使用.find("\[data-position\]"))然后循环抛出它们并获得它们的最大值来完成的。

这是一个工作片段:

&#13;
&#13;
function highestPosInBlock(posId) {
    // determine the highest number in the current block
   console.log($("[data-block="+posId+"]")) 
   var max = null;
   $("[data-block="+posId+"]").find("[data-position]").each(function(){
        var value = parseInt($(this).attr('data-position'));
       max = (value > max) ? value : max;
    });
    
    console.log(max);
    alert("Max position value of the table with data-block = "+posId+" is -> "+max)
}



highestPosInBlock(1)
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table data-block="1">
    <tr>
        <td data-position="1">1</td>
    </tr>
    <tr>
        <td data-position="2">2</td>
    </tr>
    <tr>
        <td data-position="3">3</td>
    </tr>
</table>

<table data-block="2">
    <tr>
        <td data-position="1">1</td>
    </tr>
    <tr>
        <td data-position="2">2</td>
    </tr>
</table>
&#13;
&#13;
&#13;

如果您想阻止使用find()使用for循环:

&#13;
&#13;
function highestPosInBlock(posId) {
    // determine the highest number in the current block
    var max = null;
    var tds = $("[data-block="+posId+"]").find("[data-position]");
   
    if(tds.length>0){
       for(var i=0;i<tds.length;i++) {
         var value =  parseInt($(tds[i]).attr('data-position'));
         max = (value > max) ? value : max;
       }
    }
    
    console.log(max);
    alert("Max position value of the table with data-block = "+posId+" is -> "+max)
}



highestPosInBlock(1)
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table data-block="1">
  <tr>
    <td data-position="1">1</td>
  </tr>
  <tr>
    <td data-position="2">2</td>
  </tr>
  <tr>
    <td data-position="3">3</td>
  </tr>
</table>

<table data-block="2">
  <tr>
    <td data-position="1">1</td>
  </tr>
  <tr>
    <td data-position="2">2</td>
  </tr>
</table>
&#13;
&#13;
&#13;