jQuery:对表进行排序并基于值(标记),自动选择具有最低值的单选按钮

时间:2011-10-15 21:56:41

标签: jquery

表格如何排序表格并基于值(标记),自动选择具有最低值的单选按钮

<html>
    <title></title>
    <head></head>
    <body>
    <table id="one" class="tabl" cellspacing="0" >
    <tbody>
    <tr class="row">
      <td><input class="dep" type="radio" name="depart"></td>
      <td><a class="marks">500</a></td>
    </tr>  
    <tr class="row">
      <td><input class="dep" type="radio" name="depart"></td>
      <td><a class="marks">200</a></td>
    </tr>
    <tr class="row">
      <td><input class="dep" type="radio" name="depart"></td>
      <td><a class="marks">300</a></td>
    </tr>
    <tr class="row">
      <td><input class="dep" type="radio" name="depart"></td>
      <td><a class="marks">400</a></td>
    </tr>
    </tbody>
    </table>
    </body>
</html>

1 个答案:

答案 0 :(得分:0)

$(function(){
   var table = $('#one');
   var sortMarks = function(a,b)
   {
       var aVal = Number($(a).find('a.marks').text()),
           bVal = Number($(b).find('a.marks').text());
       return aVal > bVal ? 1 : -1;
   };
   table.find('tr').sort(sortMarks).appendTo(table); // sort
   table.find('tr:first input.dep').attr('checked', 'checked'); // select first row (lowest)
});