Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 按字母顺序-返回行的索引_Jquery - Fatal编程技术网

Jquery 按字母顺序-返回行的索引

Jquery 按字母顺序-返回行的索引,jquery,Jquery,我得到了一张由td列之一排序的表。。当插入新行时,我需要获取新行将适合的索引,而不会损坏字母顺序 我这里有一个方法,但它只适用于数字。。你怎么能让它也和字符串一起工作呢 this.get_row_index = function(value, td_index){ var index = 0; td_index = td_index ? td_index:0; $('tr', this.scope.tbl_list).each(function(){ if

我得到了一张由td列之一排序的表。。当插入新行时,我需要获取新行将适合的索引,而不会损坏字母顺序

我这里有一个方法,但它只适用于数字。。你怎么能让它也和字符串一起工作呢

this.get_row_index = function(value, td_index){
    var index = 0;
    td_index = td_index ? td_index:0;
    $('tr', this.scope.tbl_list).each(function(){
        if(parseInt($('td', this).eq(td_index).html()) > value){
            return false;
        }
        index++;
    });

    return index;
};

在javascript中,字符串可以与
进行比较,假设它是排序依据的第一列,我发现这是有效的:

function sortAlpha(a,b){
    return a.innerHTML > b.innerHTML ? 1 : -1;
};

$(function() {
    $('table#thisOne tr').sort(sortAlpha).appendTo('table#thisOne');
});
..要对该表进行排序:

<table id="thisOne">
    <tr><td>Zebra</td><td>0</td></tr>
    <tr><td>Bees</td><td>11</td></tr>
    <tr><td>Apples</td><td>101</td></tr>
    <tr><td>Carrots</td><td>19</td></tr>
</table>    

斑马
啤酒11
苹果101
胡萝卜19

True,我也会使用
.text()
而不是
.html()
来避免比较html。