如何在使用Jquery单击td时将复选框标记为选中?
我有一个包含N个tr>td的表,我有class=“hrchy dt Checkboxes”的复选框,因此每当我单击该td时,都应该选中该复选框 这是我的HTML示例如何在使用Jquery单击td时将复选框标记为选中?,jquery,Jquery,我有一个包含N个tr>td的表,我有class=“hrchy dt Checkboxes”的复选框,因此每当我单击该td时,都应该选中该复选框 这是我的HTML示例 <tr style="height:20.1pt;"><input disabled="disabled" value="" class="dt-checkboxes" id="row_2" type="checkbox"> <td class="td_0_3">1<input dis
<tr style="height:20.1pt;"><input disabled="disabled" value="" class="dt-checkboxes" id="row_2" type="checkbox">
<td class="td_0_3">1<input disabled="disabled" value="" class="hrchy-dt-checkboxes" id="td_2" type="checkbox"></td>
<td class="td_0_1">A-1) Efectivo y otros activos líquidos equivalentes</td>
<td class="td_0_1">18063955860.200008</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr style="height:20.1pt;"><input disabled="disabled" value="" class="dt-checkboxes" id="row_1" type="checkbox">
<td class="td_0_3"> <input disabled="disabled" value="" class="hrchy-dt-checkboxes" id="td_1" type="checkbox"></td>
<td class="td_0_1">ACTIVO: Total del Sector</td>
<td class="td_0_1">Importe</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
从上述代码开始,每当单击复选框时,该值都将被带到后端。此代码需要增强
谢谢您可以使用下面的代码选中复选框。但是我不知道你是否也需要取消选中复选框,所以不要添加那个复选框。如果你需要,一定要告诉我
// Code goes here
$( document ).ready(function() {
$('.hrchy-dt-checkboxes').parent().parent().click(function(){
var elem = this;
var chk = $(this).find('.hrchy-dt-checkboxes')[0];
$(chk).prop('checked','checked');
})
});
这是同样的理由
更新:
以下是切换选择的更新代码:
$( document ).ready(function() {
$('.hrchy-dt-checkboxes').parent().parent().click(function(){
var elem = this;
var chk = $(this).find('.hrchy-dt-checkboxes')[0];
var isChecked = $(chk).prop('checked');
$(chk).prop('checked',!isChecked);
})
});
希望这是你想要的
$('td').click(function (event) {
if (!$(event.target).is('input')) {
$('input:checkbox', this).prop('checked', function (i, value) {
return !value;
});
}
})) jQuery代码:将复选框标记为已选中
$("td").on('click',function(){
$("this").closest("tr").find(".hrchy-dt-checkboxes").attr('checked',true);
});
更新:
if(checkboxClass.indexOf('hrchy-dt-checkboxes')!=-1){
$('.hrchy-dt-checkboxes:checked').each(function(i){
var idAttr=$(this).attr('id');
var selectedTDVal=$(this).parent().text();
var indexVal=idAttr.substring(idAttr.indexOf('_'),idAttr.length);
indexVal=indexVal.replace('_','');
var hrchyIndexId=parseInt(indexVal);
selectedVal[i]=selectedTDVal;
hrchyMap[hrchyIndexId]=selectedTDVal;
});
selectedVal=[];
}
如果复选框已选中,则添加条件,然后将其标记为未选中或已选中。使用.is(“:checked”)
我们获得复选框状态
$("table").on('click', 'td', function() {
var currentRow = $(this).closest("tr");
if (currentRow.find(".hrchy-dt-checkboxes").is(':checked')) {
currentRow.find(".hrchy-dt-checkboxes").prop("checked", false);
} else {
currentRow.find(".hrchy-dt-checkboxes").prop("checked", true);
}
});
演示:你能给我们分享一下代码片段吗?嗨@KARANLAGALWAR,我已经更新了,请检查嗨,是的,我也需要取消选中选项,你能提供给我吗!查看更新的代码。我还更新了plunker。手动(即通过鼠标单击)或编程方式选中复选框是否重要?