从小部件获取表单父级

发布于 2024-12-11 19:55:52 字数 181 浏览 0 评论 0原文

给定一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

北恋 2024-12-18 19:55:52

如果您只是寻找表单元素,那么最好找到输入(或其他表单内容元素)并使用其 form 属性。像下面这样的东西

function getForm(widget) {
   var inputNode = widget.valueNode || widget.focusNode;
   if (inputNode) return inputNode.form;
   return null;
}

适用于我看过的小部件。可能会错过一些小部件,但不要认为您会得到误报。

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

function getForm(widget) {
   var inputNode = widget.valueNode || widget.focusNode;
   if (inputNode) return inputNode.form;
   return null;
}

would work for the widgets I looked at. Might miss on some widgets, but don't think you could get a false positive.

扮仙女 2024-12-18 19:55:52

由于表单小部件也可以在表单之外使用,所以我能想到的唯一方法与没有小部件时所做的相同:获取起始 dom 节点并向上移动,直到到达表单节点:(

function find_ancestor_of_type(ancestorTag, node){
    while(node && node.tagName !== ancestorTag){
        node = node.parentNode;
    }
}

function get_form(widget){
    return fund_ancestor_of_type("form", widget.domNode);
}

虽然我还没有测试过这一点) )


顺便问一下,为什么需要这样做?大多数时候,您可以将大部分逻辑放在表单本身中(如果您愿意,它也可以是 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:

function find_ancestor_of_type(ancestorTag, node){
    while(node && node.tagName !== ancestorTag){
        node = node.parentNode;
    }
}

function get_form(widget){
    return fund_ancestor_of_type("form", widget.domNode);
}

(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.

烟花肆意 2024-12-18 19:55:52

如果表单是 dijit/form/Form,您可以使用 dijit/registry 来获取表单(小部件不仅仅是 DOM 节点)。

require( ["dijit/registry"], function( registry )
{
    var formWidget = registry.getEnclosingWidget( registry.byId('form_element_id').domNode );
} );

If the form is a dijit/form/Form, you could use dijit/registry to get the form (the widget not just the DOM node).

require( ["dijit/registry"], function( registry )
{
    var formWidget = registry.getEnclosingWidget( registry.byId('form_element_id').domNode );
} );
何时共饮酒 2024-12-18 19:55:52

对于那些好奇的人来说,这对我有用:

function getForm(widget) {

    return (widget.domNode.parentNode);
}

For those who are curious, this is what worked for me:

function getForm(widget) {

    return (widget.domNode.parentNode);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文