使用jQuery从表中获取两个最低值 [英] Get two lowest values from a table with jQuery

查看:120
本文介绍了使用jQuery从表中获取两个最低值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张桌子:

<table>
    <tr>
        <th>Name</th>
        <th>Id</th>
        <th>Value</th>
    </tr>
    <tr>
        <td>Ankaran</td>
        <td>101</td>
        <td>91</td>
    </tr>
    <tr>
        <td>Ljubljana</td>
        <td>102</td>
        <td>213</td>
    </tr>
    <tr>
        <td>Celje</td>
        <td>103</td>
        <td>711</td>
    </tr>
    <tr>
        <td>Portorož</td>
        <td>104</td>
        <td>121</td>
    </tr>
</table>

我想从最后一列中获得两个最低值(值"),并用这两个值给td标签上色.什么代码可以完成此任务?

I want to get the two lowest values from the last column ("Value") and color the td tags with these two values. What code can acomplish this?

我尝试过:

$(document).ready(function(){
    $("#btnSelect").click(function(e) {
    e.preventDefault();
        var values= new Array();
        $('tr').each(function () {
            var sorted = $(this).find('td:last').html();
            values.push(sorted);
        });
        console.log(values);
    });
});

我得到了最低的价值,但是那是我获得的最低价值,而且我也不认为这是一个可靠的解决方案

I got the lowest value, but that is about as far as I got, also I dont think this is a robust solution

推荐答案

首先,检查 考虑您的HTML,这是我的CSS& js解决方案:

Consider your HTML, here is my css & js solution:

javascript

var tds = $('td.value');
var arr = $.makeArray(tds);
arr.sort(function(a,b){
    return parseInt($(b).text()) - parseInt($(a).text());
});

$(arr[arr.length-2]).addClass('lowest');
$(arr[arr.length-1]).addClass('lowest');

css

td.lowest {background-color:lightgreen;}

这篇关于使用jQuery从表中获取两个最低值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆