Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.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
Javascript Jquery检查父项复选框_Javascript_Jquery - Fatal编程技术网

Javascript Jquery检查父项复选框

Javascript Jquery检查父项复选框,javascript,jquery,Javascript,Jquery,检查下面的代码。这里的AAA和BBB是根父菜单。我已经做的是,当我单击AAA时,它会选中所有子复选框,当我单击AAA1时,它会选中所有子复选框。但问题是,当选中一个子复选框时,它的主根父菜单也应该被选中,而这不起作用。检查Jquery部分。我已经像$(this).parent().parent().parent().parent().find('.parent_checkbox').prop(“选中”,this.checked)一样尝试过了但这不会选择其根父级。我怎样才能修好它 Jquery:

检查下面的代码。这里的
AAA
BBB
是根父菜单。我已经做的是,当我单击
AAA
时,它会选中所有子复选框,当我单击
AAA1
时,它会选中所有子复选框。但问题是,当选中一个子复选框时,它的主根父菜单也应该被选中,而这不起作用。检查Jquery部分。我已经像
$(this).parent().parent().parent().parent().find('.parent_checkbox').prop(“选中”,this.checked)一样尝试过了但这不会选择其根父级。我怎样才能修好它

Jquery:

$("body").on("change", ".parent_checkbox", function () {
        $(this).parent().parent().parent().parent().find('.child1_checkbox').prop("checked", this.checked);
        $(this).parent().parent().parent().parent().find('.child2_checkbox').prop("checked", this.checked);
    });
    $("body").on("change", ".child1_checkbox", function () {
        $(this).parent().parent().parent().parent().find('.child2_checkbox').prop("checked", this.checked);
    });
    $("body").on("change", ".child2_checkbox", function () {
        console.log(this);
        $(this).parent().parent().parent().parent().find('.parent_checkbox').prop("checked", this.checked);
    });
Html:


我建议你看看parents()和closest()。使用closest(),可以搜索触发已知事件的元素的最近父元素,并使用该父元素查找另一项:

$('body').on('change', 'input', function() {
  $(this).closest('.the-parent-that-i-need').find('.another-item').doStuff();
});

正如@Frenchy所描述的那样,我必须搜索外部家长。我像下面那样修正了这个问题:

$(this).closest('.EachCategorySet').find(".parent_checkbox").prop("checked", this.checked); 

AAA不是AAA1、BBB1和CCC html的父项,speakingaha@Frenchy是正确的
AAA
在另一个分区中。无论如何,如何修复它呢?
$(this).closest('.EachCategorySet').find(".parent_checkbox").prop("checked", this.checked);