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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.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选中并取消选中表tr的复选框_Jquery_Checkbox_Html Table - Fatal编程技术网

使用jQuery选中并取消选中表tr的复选框

使用jQuery选中并取消选中表tr的复选框,jquery,checkbox,html-table,Jquery,Checkbox,Html Table,当单击表行(tr)时,我试图选中该复选框。并获取复选框的数据值,并在表中显示为选定的总额,在显示选定的总额之前,有一个条件检查 如果((所选金额+所选总金额)partyTotAmount){ 警报('如果您选择此支票,则超过最高应付金额!'); }否则{ $(this).closest('tr').find('input[type=checkbox]).prop('checked',true); $(this).closest('tr').css('background-color','rgba

当单击表行(tr)时,我试图选中该复选框。并获取复选框的数据值,并在表中显示为选定的总额,在显示选定的总额之前,有一个条件检查

如果((所选金额+所选总金额)partyTotAmount){ 警报('如果您选择此支票,则超过最高应付金额!'); }否则{ $(this).closest('tr').find('input[type=checkbox]).prop('checked',true); $(this).closest('tr').css('background-color','rgba(3,60,107,0.99)); $(this).closest('tr').css('color','#fff'); } } $('.hiddnClas').remove(); $(“.checked:checked”).each(函数(){ 计数+=1; sum+=parseFloat($(this.data('value')); }); $('paySum').val(parseFloat($('partyTotAmount').val()-sum).toFixed(2)); $('selcotatamount').val(总和固定值(2)); $('totChqRow').val(计数); $('#chqcount').val(计数); });
事件被触发两次的原因是您在复选框中选中了class
,以及
tr
。复选框中可能不需要此类。如果删除它,事件只触发一次。

由于复选框单击事件干扰tr的单击事件,最好稍微修改逻辑

首先,将所有tr的类更改为可检查的,这样您就可以说“这些元素可以检查或切换”

现在,您可以将条件块更改为首先检查tr上是否存在
checked
类,然后单击并运行正确的代码,同时根据需要添加/删除
checked

if($(this).closest('tr').find("input[type=checkbox]").prop('checked')){ ...
变成:

if($(this).hasClass('checked')){ ...
当您为这两种状态应用样式时,可以使用
$(this.removeClass('checked')切换选中的类
$(this).addClass('checked')。(您可以使用
$(this).toggleClass('checked');
,但我更喜欢显式的


这里有一个。

那么复选框不起作用了。我已经试过了,它不起作用了,请您通过JSFIDLE检查代码。@GaneshKumar当点击复选框时,它不起作用了,因为jquery似乎没有读到
.prop(“选中”);
正确。@GaneshKumar感谢您的努力,但当我单击复选框时,它无法正常工作
$(document).on('click', 'tr.checkable', function(){ ...
if($(this).closest('tr').find("input[type=checkbox]").prop('checked')){ ...
if($(this).hasClass('checked')){ ...