谁能告诉我为什么这个jQuery脚本不起作用?

Ahm*_*uad 2 javascript jquery

这是我的代码.

function switchbox(showall, maincls) {
    $(showall).change(function() {
        if ($(this).is(":checked")) {
        $(maincls).each(function(i) {
            $(this).attr('disabled', true);
        });
        } else {
        $(maincls).each(function(i) {
            $(this).attr('disabled', false);
        });
        }
    });
}

switchbox('.show-all-tags, .tags');
switchbox('.show-all-cats, .categories');
Run Code Online (Sandbox Code Playgroud)

如果我没有在函数内使用变量,它可以工作.它应该在选中show-all-X时禁用该类的所有复选框,反之亦然.我尝试了没有变量showall,maincls它的工作原理.我在这做错了吗?

谢谢.

Ash*_*ley 7

maincls没有被定义,也许你的意思是:

switchbox('.show-all-tags', '.tags');
switchbox('.show-all-cats', '.categories');
Run Code Online (Sandbox Code Playgroud)