jQuery查找选中的所有行

jQuery Find all rows that checked

本文关键字:查找 jQuery      更新时间:2023-09-26

我有一个带有复选框的表。我需要知道检查了哪些表行,单击"更新"按钮后。

 <div id='ggg' style="position:absolute; bottom:0px" id=>
    <table class="display" id="poll">
    <th>Polling</th>
            <th>rrd</th>
<tr>
  <td>ping</td>
  <td>10.rrd</td>   
<td>
            <input type="checkbox" name="myTextEditBox" value="checked"/> 
        </td>  
  </tr>
<tr>
  <td>snmp</td>
  <td>11.rrd</td>   
<td>
            <input type="checkbox" name="myTextEditBox" value="checked" /> 
        </td>  
</tr>
</table>
<form>
<input type='button' id='update' value='update'>
</form>
    </div>

试试这个

$('#update').on('click',function(){
    $('#poll').find('input:checked').closest('tr').css('background','green');
});

DEMO

您可以使用:checked选择器来获取复选框,同时使用.closest(tr)来获取行。试试这个:

checkedrows = $('input:checked').closest('tr');

试试这个

$("#update").click(function () {
    $('input:checked').each(function () {
        alert($(this).closest("tr").text());
    });
});

演示

编辑

$("#update").click(function () {
    $("#poll").find('input:checked').each(function () {
        alert($(this).closest("tr").text());
    });
});

您可以这样使用:

如果你想突出显示到tr:

$('#update').on('click',function(){
    $(':checked').parents('tr').css('background','#ff0');
});

如果你想突出显示到td:

$('#update').on('click',function(){
    $(':checked').parent().css('background','#ff0');
});
$('#update').click(function() {
 var checkboxes = $('#poll').find('input:checked').closest('tr');
 // this will give you which table rows was checked    
});
相关文章: