我目前有一个JSTree显示其中包含复选框的列表项。
我的目标:将选定的列表项值发送到PHP POST表单。
这就是我在我的网站上回应它们的方式:
echo '<li id="dynamicID"><input name="checkboxname" type="checkbox" id="SameIdThanListItemID" value="checkboxvalue">List item text</li>
这工作正常,我的列表项目按预期显示。
我们先得到列表项检查,然后是JSTree图标,然后选中我必须用于帖子提交的复选框输入(此复选框将被隐藏)。无法单击复选框,我认为这是由于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>
标签内,则无法检查/取消选中复选框。
为了解决这个问题,我在树外做了一个循环,并在隐藏的div中回显了每个复选框。现在选择列表项也会检查正确的复选框,以便帖子提交按预期工作。