从小部件获取表单父级
给定一个 dojo 输入小部件(任何表单小部件),获取小部件的表单元素的最佳方法是什么?即父元素。
即我希望完成这样的功能:
function getForm(widget) {
return ( /**code goes here **/);
}
谢谢
Given a dojo input widget (any form widget), what's the best way to obtain the widget's form element? i.e. parent element.
i.e. I am looking to complete a function like this:
function getForm(widget) {
return ( /**code goes here **/);
}
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您只是寻找表单元素,那么最好找到输入(或其他表单内容元素)并使用其 form 属性。像下面这样的东西
适用于我看过的小部件。可能会错过一些小部件,但不要认为您会得到误报。
If you are simply looking for the form element, you might be better off finding the input (or other form content element) and use its form property. Something like the following
would work for the widgets I looked at. Might miss on some widgets, but don't think you could get a false positive.
由于表单小部件也可以在表单之外使用,所以我能想到的唯一方法与没有小部件时所做的相同:获取起始 dom 节点并向上移动,直到到达表单节点:(
虽然我还没有测试过这一点) )
顺便问一下,为什么需要这样做?大多数时候,您可以将大部分逻辑放在表单本身中(如果您愿意,它也可以是 dijit.form.Form),并且输入小部件不需要了解任何有关它的信息。
Since form widgets can also be used outside forms, the only way I can think of is the same as you would do without widgets: get a starting dom node and move up until you get to a form node:
(I havent tested this yet though)
By the way, why do you need to do this? Most of the times you can put most of the logic in the form itself (it can also be a dijit.form.Form if you want) and the input widgets don't need to know anything about it.
如果表单是 dijit/form/Form,您可以使用 dijit/registry 来获取表单(小部件不仅仅是 DOM 节点)。
If the form is a dijit/form/Form, you could use dijit/registry to get the form (the widget not just the DOM node).
对于那些好奇的人来说,这对我有用:
For those who are curious, this is what worked for me: