从 JStree 复选框列表创建 HTML 发布表单的值
我刚刚开始使用 JQuery 和 Javascript,并调整一组嵌套的复选框输入以利用 jstree 复选框行为。在意识到每个项目必须是一个锚标记才能被 jstree 代码识别之后,我现在不确定如何从 POST 表单提交的项目生成值。以前它们是复选框输入,我希望能够保持这种方式,以便在关闭 JavaScript 的情况下表单仍然可以运行。将输入包装在锚标记中会导致每个项目有两个复选框,一个由 JStree 进行样式和行为控制,另一个则被忽略。我认为这不是要走的方向。
任何有关如何实现这一目标的建议将不胜感激。
我使用以下配置代码启动 JStree -
$(document).ready(function()
{
$('.nested-category').jstree({
ui: { theme_name : "checkbox" },
plugins : ["checkbox", "themes", "html_data", "ui" ]
});
}
);
这是 JStree 代码影响的表单的简化版本,但无法提交值。
<form method="post" action="">
<div class="nested-category">
<ul class="">
<li><a href="#">group1</a>
<ul>
<li><a href="#">subgroup1</a>
<ul>
<li><a href="#">item1</a></li>
<li><a href="#">item2</a></li>
<li><a href="#">item3</a></li>
</ul>
</li>
<li><a href="#">subgroup2</a>
<ul>
<li><a href="#">item4</a></li>
<li><a href="#">item5</a></li>
<li><a href="#">item6</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#">group2</a>
<ul>
<li><a href="#">subgroup3</a>
<ul>
<li><a href="#">item7</a></li>
<li><a href="#">item8</a></li>
<li><a href="#">item9</a></li>
</ul>
</li>
<li><a href="#">subgroup4</a>
<ul>
<li><a href="#">item10</a></li>
<li><a href="#">item11</a></li>
<li><a href="#">item12</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
<input class="form-submit" type="submit" name="submit" value="submit">
</form>
I am just starting out with JQuery and Javascript, and adapting a nested set of checkbox inputs to make use of jstree checkbox behaviours. After realising that each item had to be an anchor tag to be recognised by the jstree code, I am now unsure how to generate values from the items for a POST form submission. Previously they were checkbox inputs, and I would like to be able to keep them this way so that the form would still function if Javascript were turned off. wrapping inputs in the anchor tag resulted in two checkboxes for each item, one styled and behaviourally controlled by JStree, the other ignored. I assume this isn't the direction to go.
Any advice on how to achieve this would be gratefully received.
I am booting JStree with the following config code -
$(document).ready(function()
{
$('.nested-category').jstree({
ui: { theme_name : "checkbox" },
plugins : ["checkbox", "themes", "html_data", "ui" ]
});
}
);
Here is a simplified version of the form which the JStree code effects, but which can't submit values.
<form method="post" action="">
<div class="nested-category">
<ul class="">
<li><a href="#">group1</a>
<ul>
<li><a href="#">subgroup1</a>
<ul>
<li><a href="#">item1</a></li>
<li><a href="#">item2</a></li>
<li><a href="#">item3</a></li>
</ul>
</li>
<li><a href="#">subgroup2</a>
<ul>
<li><a href="#">item4</a></li>
<li><a href="#">item5</a></li>
<li><a href="#">item6</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#">group2</a>
<ul>
<li><a href="#">subgroup3</a>
<ul>
<li><a href="#">item7</a></li>
<li><a href="#">item8</a></li>
<li><a href="#">item9</a></li>
</ul>
</li>
<li><a href="#">subgroup4</a>
<ul>
<li><a href="#">item10</a></li>
<li><a href="#">item11</a></li>
<li><a href="#">item12</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
<input class="form-submit" type="submit" name="submit" value="submit">
</form>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通过在每个锚标记内添加输入元素解决了这个问题,这些元素被一些额外的 Javascript 隐藏。当检查 jsTree 生成的输入时,jsTree 脚本中的几行附加行将“checked”添加到输入,反之亦然(如果输入返回到已检查的表单)。如果关闭 Javascript,输入复选框将在 HTML 表单中正常工作。如果有人需要进一步解释,我可以发布示例以及对 jsTree 所做的更改。
我不怀疑这是一种有点摇摇欲坠的解决问题的方法,但这就是我目前对网络开发的不了解状态!
I solved the issue by adding input elements inside each anchor tag, which are hidden by a bit of additional Javascript. A couple of additional lines in the jsTree script add 'checked' to the inputs when the jsTree generated input is checked, and vice versa if the input is returned to the form checked. If Javascript is switched off, the input checkboxes work as normal in an HTML form.I can post examples and the changes made to jsTree if anyone would like further explanation.
I don't doubt this is a bit of a ramshackled way to solve the problem, but that's where I'm at in my currently unenlightened state of web development!
就我个人而言,我会使用 AJAX。
$.post
给锚点一个 ID,在 jsTree 中我确信有一个方法可以获取所有选定的 ID
personally, i would use AJAX.
$.post
give the anchors an ID, and in jsTree i'm sure there is a method that gets all selected ID's