在 JSTree 中选择列表项时选择复选框

Selecting Checkbox when Selecting List Item in JSTree

我目前有一个 JSTree,它显示带有复选框的列表项。

我的目标:将选定的列表项值发送到 PHP POST 表单。

这是我在我的网站上回应他们的方式:

echo '<li id="dynamicID"><input name="checkboxname" type="checkbox" id="SameIdThanListItemID" value="checkboxvalue">List item text</li>

这工作正常,我的列表项按预期显示。

我们首先检查列表项,然后是 JSTree 图标,然后是我必须用于 post 提交的复选框输入(此复选框将被隐藏)。无法单击复选框,我认为这是由于 JSTree 行为造成的。

树目前设置为禁用多选,因此我们没有遇到与数组相关的问题。我们绝不会处理超过一个 ID。

当我尝试在单击列表项时选中复选框时出现问题。

    $("#tree").bind("changed.jstree",

        function (e, data) {       

        var nodeId = $('#tree').jstree().get_selected("id")[0].id;

        // Shows up the proper ID selected, and both checkbox and 
        // list item have the same ID, so we're good untill here
        console.log(nodeId); 

        // We've got the ID properly stored into nodeId,and we've checked it with console.Log, 
        // however, the input checkbox its never getting selected.
        document.getElementById(nodeId).checked = true;

        }
    );

我发现如果复选框在 JSTree <li></li> 标签内,它们将不能 checked/unchecked。

为了解决这个问题,我只是在树外做了一个循环,并回显了隐藏 div 中的每个复选框。现在选择列表项也会检查正确的复选框,因此 post 提交按预期工作。