Javascript jQuery自动触发复选框事件

Javascript jQuery自动触发复选框事件,javascript,javascript-events,jquery,Javascript,Javascript Events,Jquery,我希望不是手动触发事件,而是通过jQuery本身触发事件。 以下是更好地理解我的问题: $('.commonCheck').change(function () { if ($(".cashierCheck").attr("checked")) { $('.cashierInput').fadeIn(); console.log("________________hr") }


我希望不是手动触发事件,而是通过jQuery本身触发事件。
以下是更好地理解我的问题:

$('.commonCheck').change(function () {
            if ($(".cashierCheck").attr("checked")) {
                $('.cashierInput').fadeIn();
                console.log("________________hr")
            }
            else{
                $('.cashierInput').fadeOut();
                console.log("_______________uncjecked")
            }
}
我的复选框:

<input type="checkbox" name="cashierFilter" id="cashierFilter" class="cashierCheck commonCheck" class="mrL0" />
演示

您可以使用
触发器
以编程方式触发事件:

$('#cashierFilter').prop('checked', true).trigger('change');
此外,还应使用
is
检查元素的属性,并且可以在变量中缓存选择器以提高性能:

$('.commonCheck').change(function () {
    var $cashierCheck = $(".cashierCheck");
    if ($cashierCheck.is(":checked")) {
        $cashierCheck.fadeIn();
        console.log("________________hr")
    }
    else {
        $cashierCheck.fadeOut();
        console.log("_______________uncjecked")
    }
}
演示

您可以使用
触发器
以编程方式触发事件:

$('#cashierFilter').prop('checked', true).trigger('change');
此外,还应使用
is
检查元素的属性,并且可以在变量中缓存选择器以提高性能:

$('.commonCheck').change(function () {
    var $cashierCheck = $(".cashierCheck");
    if ($cashierCheck.is(":checked")) {
        $cashierCheck.fadeIn();
        console.log("________________hr")
    }
    else {
        $cashierCheck.fadeOut();
        console.log("_______________uncjecked")
    }
}
在jquery中使用“是”是最佳解决方案:

$('.commonCheck').change(function () {
    if ($(".cashierCheck").is(":checked")) {
                $('.cashierInput').fadeIn();
                console.log("________________hr")
            }
            else{
                $('.cashierInput').fadeOut();
                console.log("_______________uncjecked")
            }
});
在jquery中使用“是”是最佳解决方案:

$('.commonCheck').change(function () {
    if ($(".cashierCheck").is(":checked")) {
                $('.cashierInput').fadeIn();
                console.log("________________hr")
            }
            else{
                $('.cashierInput').fadeOut();
                console.log("_______________uncjecked")
            }
});

Bruvoo,添加了一个演示,请随意使用,很抱歉我碰了你的帖子哟<代码>:)无论如何都要大声叫喊。“demo*带有
文档。准备好了
@Tats\u innint nice one,我的另一位母亲的弟弟。Bruvoo,添加了一个演示,请随意使用,抱歉我碰了你的帖子哟!
:)
不管怎样。demo*带有
文档。准备好了
@Tats\u innint nice one,我的另一位母亲的弟弟。