Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.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中的属性集时不触发复选框更改事件_Javascript_Jquery_Html_Checkbox - Fatal编程技术网

选中javascript中的属性集时不触发复选框更改事件

选中javascript中的属性集时不触发复选框更改事件,javascript,jquery,html,checkbox,Javascript,Jquery,Html,Checkbox,当我手动选中/取消选中复选框时 $('input[type=checkbox]')。更改(function(){})将完美触发 但是当我在代码中设置checked属性时,这个事件处理程序并没有得到执行 标记: <li><input id="p-Client-Management" type="checkbox" name="selectedMenuItem" value="Client Management">Client Management</input

当我手动选中/取消选中复选框时

$('input[type=checkbox]')。更改(function(){})
将完美触发

但是当我在代码中设置checked属性时,这个事件处理程序并没有得到执行

标记:

    <li><input id="p-Client-Management" type="checkbox" name="selectedMenuItem" value="Client Management">Client Management</input>
        <ul>
           <li><input class="parent-p-Client-Management" type="checkbox" name="selectedMenuItem" value="Demo Client">Demo Client</input></li>
           <li><input class="parent-p-Client-Management" type="checkbox" name="selectedMenuItem" value="Basic Clients">Basic Clients</input></li>
        </ul>
    </li>

    **Event handler :**

    $('input[type=checkbox]').change(function(){
                    var id = $(this).attr('id');

                    if(id){  
                        console.log("parent found -> "+id);
                        var children = $('.parent-'+id);
                        console.log("children of "+id+" -> "+children.length);
                        for(var i = 0; i<children.length; i++){
                            children[i].checked = document.getElementById(id).checked;
                        }
                    }
                    else{
                        if(this.checked){
                            console.log("Child is checked");
                            var suffix = $(this).attr('class').split("-")[2];
                            document.getElementById('p-'+suffix).checked = true;
                        }

});
  • 客户端管理
    • 演示客户端
    • 基本客户
  • **事件处理程序:** $('input[type=checkbox]')。更改(函数(){ var id=$(this.attr('id'); 如果(id){ console.log(“找到父项->”+id); var children=$('.parent-'+id); console.log(“子项的“+id+”->“+childrence.length”);
    对于(var i=0;i以编程方式更改元素的值/已检查状态不会触发更改处理程序,但您可以使用
    .trigger('change')
    或使用缩写
    .change()
    手动触发它们

    试试这个,这是答案


    这应该是在页面加载时发生的吗?如果是,请将代码包装在
    $(函数(){…});
    中。如果(this.checked),您还必须使用
    功能超出了您的输入更改范围。为什么在使用JavaScript选中/取消选中复选框时不会触发该复选框的
    更改
    事件?只有在用户启动时才会触发事件…您可以手动触发事件…
    $(复选框)。prop('checked',true)。trigger('change'))
    如果我不使用jQuery?
    checkboxElement.dispatchEvent(新事件('change'));
    ,对吗?
    $('input[type=checkbox]').change(function () {
        var id = $(this).attr('id');
    
        if (id) {
            console.log("parent found -> " + id);
            var children = $('.parent-' + id);
            console.log("children of " + id + " -> " + children.length);
    
            if (children.length) {
                children[this.checked ? 'not' : 'filter'](':checked').prop('checked', this.checked).change()
            }
        } else {
            if (this.checked) {
                console.log("Child is checked");
                var suffix = $(this).attr('class').split("-")[2];
                $('#p-' + suffix).not(':checked').prop('checked', true).change();
            }
        }
    });
    
      $('input[type=checkbox]').unbind('change').change(function(){
            var id = $(this).attr('id');
            if(id=="p-Client-Management"){  
                console.log("parent found -> "+id);
                var childrens = $('.parent-'+id);
                var isChecked = $(this).is(':checked');
                childrens.prop('checked', isChecked);
                childrens.change();
            } else{
                console.log("Child is checked");
    
            }                
        });