[英]How to select all checkboxes with a class name inside a table by table id
我的页面上有几个表,还有一个ID为workOneTable的表。 我在表中有几行,其类名为.rsvLine,还有更多行,其类名为.vtoLine。 我需要能够选择类名称为.rsvLine的workOneTable中的所有复选框。 我已经尝试了几件事。
$( '#workOneTable :checkbox.rsvLine' ).prop( 'checked', this.checked );
$( '#workOneTable' ).find( 'input[type=checkbox]' ).prop( 'checked', true );
$( '#workOneTable tbody .rsvLine :checkbox' ).prop( 'checked' );
任何帮助是极大的赞赏。
编辑
这是html代码中最相关的部分。
<table id=workOneTable>
<thead>
several rows here
</thead>
<tbody>
<tr>
<td class="rsvLine"><input type="checkbox" ></td>
<td class="rsvLine"><input type="checkbox" ></td>
<td class="rsvLine"><input type="checkbox" ></td>
<td class="vtoLine"><input type="checkbox" ></td>
<td class="vtoLine"><input type="checkbox" ></td>
<td class="vtoLine"><input type="checkbox" ></td>
<td class="rsvLine">other stuff</td>
<td class="rsvLine">other stuff</td>
</tr>
</tbody>
如果不使用表ID,则可以选择合适的表。 它将在所有表中选择.rsvLine。 这是我用于此的代码。
$( '.rsvLine :checkbox' ).each( function ()
{
if ( !$( this ).prop( "checked" ) )
{
this.click();
}
} );
你需要这个
$( "#workOneTable .rsvLine input[type=checkbox]" ).each(function() {
$( this ).prop('checked', true);
});
这是一个例子
$( "#workOneTable .rsvLine input[type=checkbox]" ).each(function() { $( this ).prop('checked', true); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id=workOneTable> <thead> several rows here </thead> <tbody> <tr> <td class="rsvLine"><input type="checkbox" ></td> <td class="rsvLine"><input type="checkbox" ></td> <td class="rsvLine"><input type="checkbox" ></td> <td class="vtoLine"><input type="checkbox" ></td> <td class="vtoLine"><input type="checkbox" ></td> <td class="vtoLine"><input type="checkbox" ></td> <td class="rsvLine">other stuff</td> <td class="rsvLine">other stuff</td> </tr> </tbody> </div>
如果你正在尝试设置.checked
属性为当前的相反Boolean
值.checked
财产的使用!
操作者在前的this.checked
在.prop()
值
$(function() { $('#workOneTable .rsvLine :checkbox') .each(function() { $(this).prop('checked', !this.checked) }); })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id="workOneTable"> <thead> several rows here </thead> <tbody> <tr> <td class="rsvLine"><input type="checkbox"></td> <td class="rsvLine"><input type="checkbox"></td> <td class="rsvLine"><input type="checkbox"></td> <td class="vtoLine"><input type="checkbox"></td> <td class="vtoLine"><input type="checkbox"></td> <td class="vtoLine"><input type="checkbox"></td> <td class="rsvLine">other stuff</td> <td class="rsvLine">other stuff</td> </tr> </tbody> </table>
您无需使用“每种”方法。 这就够了:
$("#workOneTable td.rsvLine :checkbox").prop("checked", true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.