复选框操作 [英] Checkbox manipulation

查看:77
本文介绍了复选框操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某种原因,这个小代码阻止用户检查实际的复选框并让它在其中加上复选标记,检查它的唯一方法是单击该行。

For some reason this little code is preventing the user from being able to check the actual checkbox and having it put the checkmark inside of it and the only way to get it checked is to click the row.

$('table tr').click(function() {

    checkBox = $(this).children('td').children('input[type=checkbox]');

    if(checkBox.attr('checked'))
        checkBox.removeAttr('checked');
    else
        checkBox.attr('checked', 'checked');

});


推荐答案

您要禁用默认复选框行为的原因输入是复选框在 tr 内,所以当你选中复选框时,你激活你的Javascript并切换复选框......导致什么都没发生。您必须检查 您的活动目标 是否不是< a href =http://api.jquery.com/checkbox-selector/ =nofollow> :复选框

The reason you are disabling the default checkbox behavior of the input is that the checkbox is inside the tr, so when you check the checkbox you fire your Javascript AND you toggle the checkbox... leading to nothing happening. You must check that the target of your event is not a :checkbox.

此外,没有必要担心复选框的属性...使用jQuery,你可以简单地 .click() 它!

Also, there's no need to worry about the attribute of the checkbox... with jQuery, you can simply .click() it!

$('table tr').click(function(event) {      

    if (! $(event.target).is("input:checkbox"))
        $(this).find('input:checkbox').click();        
});

工作示例

这篇关于复选框操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆