javascript - jsTree复选框事件未触发

标签 javascript jstree

我无法触发复选框事件,特别是 enable_checkboxdisable_checkbox

我初始化 jsTree 的代码是:

$('#contenteditor-hierarchy').jstree({
    "plugins": [
        "checkbox"
    ],
    "checkbox": {
        "visible": true,
        "tie_selection": false,
        "whole_node": false
    },
    "core": {
        "check_callback": true,
        "data": {
            "url": function (node) {
                //...
            },
            "type": "get",
            "data": function (node) {},
            "dataType": 'json'
        }
    }
});

我试过:

$tree.bind('enable_checkbox.jstree', function() {
    alert('test');
});

// and...
$('#contenteditor-hierarchy').on('enable_checkbox.jstree', function() {
    alert('test');
});

// as well as..
$(document).on('enable_checkbox.jstree', function() {
    alert('test');
});

暂时来说,一个不太优雅的 hack;以下对我有用:

$('body').on('click', '.jstree-checkbox', function(e) {
    // at the time of this event firing, jstree hadn't finished handling the click event
    // so I had to timeout....
    setTimeout(function() {
        console.log($tree.jstree(true).get_checked());
    }, 200);
});

然而,两次尝试都没有真正触发警报。

API 文档非常模糊,所以想知道是否有人知道我哪里出错了。

最佳答案

根据带有点击事件和 setTimeout 的代码,我会说您想要实现的是设置一个事件来检测复选框何时被选中或未选中。

如果是这种情况,您应该使用事件 check_node.jstreeuncheck_node.jstree分别。

关于javascript - jsTree复选框事件未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56618133/

相关文章:

javascript - JavaScript 中两个日期之间的月差

javascript - 将节点从一个 jstree 移动到另一个 jstree 不起作用

javascript - jQuery 替换为变量不起作用

javascript - lunr.js 添加关于索引记录的数据

javascript - Google Map JS API 3 需要 API key 吗?

jquery - Jstree - set_id 函数不起作用

javascript - JsTree - 选择父节点将导致浏览器卡住

javascript - JSTree 添加新的父节点或子子节点

jquery - 如何选择 parent 和孙子?

javascript - 拼接不适用于 Angularjs