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/8/vim/5.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 取消选中除此之外的所有其他复选框,并从中删除css类_Jquery_Jquery Selectors_Checkbox - Fatal编程技术网

Jquery 取消选中除此之外的所有其他复选框,并从中删除css类

Jquery 取消选中除此之外的所有其他复选框,并从中删除css类,jquery,jquery-selectors,checkbox,Jquery,Jquery Selectors,Checkbox,我有一个带有复选框的表格行,当我单击该行时,该行将接受类“selected”,复选框变为selected $("#emp_grid tbody tr").click(function (e) { if (e.target.type == "checkbox") { // stop the bubbling to prevent firing the row's click event e.stopPropagation(); } else

我有一个带有复选框的表格行,当我单击该行时,该行将接受类“selected”,复选框变为selected

    $("#emp_grid tbody tr").click(function (e) {
    if (e.target.type == "checkbox") {

        // stop the bubbling to prevent firing the row's click event
        e.stopPropagation();
    } else {
        var $checkbox = $(this).find(':checkbox');
        $checkbox.attr('checked', !$checkbox.attr('checked'));
        $(this).filter(':has(:checkbox)').toggleClass('selected');              


    }
});

如何使所有其他复选框取消选中并从除所选类之外的行中删除“所选”类

使用您当前的脚本,类似这样的东西应该可以工作

$("#emp_grid tbody tr").click(function(e) {
    $("#emp_grid tbody tr").removeClass("selected");
    $("#emp_grid :checkbox").not(e.target).removeAttr("checked");
    if (e.target.type == "checkbox") {

        // stop the bubbling to prevent firing the row's click event
        e.stopPropagation();
    } else {

        var $checkbox = $(this).find(':checkbox');
        $checkbox.attr('checked', !$checkbox.attr('checked'));
        $(this).filter(':has(:checkbox)').toggleClass('selected');
    }
});

不确定最终的标记,但听起来您只想选择一个复选框?如果是这样,也许单选按钮列表比复选框列表更合适

更新
要允许在行上切换复选框,请单击,您可以尝试以下操作:

$("#emp_grid tbody tr").click(function(e) {
    $("#emp_grid tbody tr").removeClass("selected");
    var $checkbox = $(this).find(':checkbox');
    $("#emp_grid :checkbox").not($checkbox).removeAttr("checked");
    if (e.target.type == "checkbox") {

        // stop the bubbling to prevent firing the row's click event
        e.stopPropagation();
    } else {
        $checkbox.attr('checked', !$checkbox.attr('checked'));
        $(this).filter(':has(:checkbox)').toggleClass('selected');
    }
});

没错,通常这是一个典型的单选按钮功能,但我的雇主还是希望用复选框来实现。顺便说一句,它没有帮助:(奇怪的是,它在JSFIDLE上的代码示例中工作得很好,但在我的页面中却没有:(顺便问一下,另一个问题是:我是否可以只在单击复选框时使用相同的功能well@wallace740,将您的标记插入JSFIDLE,我可以看看发生了什么。很抱歉,它起作用了,我的不好。但是,如果选择一次,则不会取消选择,至少有一行始终处于选中状态。而不是“$”(“#“+id+”:checkbox“).not(e.target).removeAttr(“checked”);“这也可以切换吗?@wallace740,请参阅更新的fiddle,应该注意复选框的切换。